Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

how should i detect mouse click on JTextField

  Asked By: Craig    Date: Dec 29    Category: Java    Views: 9158
  

I want do some opration on JTextField when

Click on JextField how should i do that

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Katrina Edwards     Answered On: Dec 29


For detecting clicks inside a JTextField instance, you would simply add a
mouse-listener or
mouse-adapter to your instance.

This might help:
----------------


package org.blahblah;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ClickOnTextField {

public static void main(String[] args) {

JFrame frame = new JFrame();
JTextField field = new JTextField();

frame.add(field);

field.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked");
}
});

frame.pack();
frame.setVisible(true);
}

}

 
Answer #2    Answered By: Eddie Austin     Answered On: Dec 29

you could use this sample :


import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
This listener simply prints out the listener method name
and the x- and y-coordinate of the mouse  position.
*/
public class MouseSpy implements MouseListener
{
public void mousePressed(MouseEvent event)
{
System.out.println("Mouse pressed. x = "
+ event.getX() + " y = " + event.getY());
}
public void mouseReleased(MouseEvent event)
{
System.out.println("Mouse released. x = "
+ event.getX() + " y = " + event.getY());
}
public void mouseClicked(MouseEvent event)
{
System.out.println("Mouse clicked. x = "
+ event.getX() + " y = " + event.getY());
}
public void mouseEntered(MouseEvent event)
{
System.out.println("Mouse entered. x = "
+ event.getX() + " y = " + event.getY());
}
public void mouseExited(MouseEvent event)
{
System.out.println("Mouse exited. x = "
+ event.getX() + " y = " + event.getY());
}
}
ExP10_1.java

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
/**
This programs demonstrates the use of frames to
display mouse events
*/
public class ExP10_1 extends JFrame
{
public static void main(String args[])
{
JFrame frame = new JFrame();
MouseSpy listener = new MouseSpy();
frame.addMouseListener(listener);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}
P10.3
ExP10_3.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.Random;
/**
This applet lets the user select a number of rectangles
and displays them in random positions
*/
public class ExP10_3 extends Applet
{
public ExP10_3 ()
{
// the rectangle that the paint method draws
box = new Rectangle(BOX_X, BOX_Y,
BOX_WIDTH, BOX_HEIGHT);
// the text field for entering a number of rectangles
final JTextField nField = new JTextField(5);

// the button to draw the rectangle
JButton drawButton = new JButton("Draw");
class DrawButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
n = Integer.parseInt(nField.getText());

repaint();

// clears the text field

nField.setText("");
}
}
ActionListener listener = new DrawButtonListener();
drawButton.addActionListener(listener);
// the labels for labeling the text fields
JLabel nLabel = new JLabel("Number of Rectangles = ");
// the panel for holding the user interface components
JPanel panel = new JPanel();
panel.add(nLabel);
panel.add(nField);
panel.add(drawButton);
// the frame for holding the component panel
JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
frame.show();
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;

Random generator = new Random();

int x = 0;
int y = 0;

for (int i = 0; i < n; i++)
{
x = generator.nextInt(getWidth());
y = generator.nextInt(getHeight());
box.setLocation(x, y);
g2.draw(box);
}
}
private Rectangle box;
private int n;
private static final int BOX_X = 100;
private static final int BOX_Y = 100;
private static final int BOX_WIDTH = 20;
private static final int BOX_HEIGHT = 30;
}
P10.5
ExP10_5.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
This applet lets the user move a circle by specifying
the radius and clicking on the center.
*/
public class ExP10_5 extends Applet
{
public ExP10_5()
{
// the circle that the paint method draws
circle = new Ellipse2D.Double();
// the text fields for the radius
final JTextField rField = new JTextField(5);

// add mouse press listener
class MousePressListener implements MouseListener
{
public void mousePressed(MouseEvent event)
{
double radius = Double.parseDouble(rField.getText());
int x = event.getX();
int y = event.getY();
circle.setFrame(x - radius, y - radius,
2 * radius, 2 * radius);
repaint();
}
// do-nothing methods
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}

MouseListener listener = new MousePressListener();
addMouseListener(listener);
// the label for labeling the text field
JLabel rLabel = new JLabel("Radius = ");
// the panel for holding the user interface components
JPanel panel = new JPanel();
panel.add(rLabel);
panel.add(rField);
// the frame for holding the component panel
JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
frame.show();
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.draw(circle);
}
private Ellipse2D.Double circle;
}
P10.7
Triangle.java

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
This class provides a method to draw a triangle
from three user-specified points
*/
public class Triangle
{
/**
Construct a Triangle object
@param aClick the number of clicks the user pressed
@param aPoint the point that the user pressed on
*/
public Triangle(int aClick, Point2D.Double[] aPoint)
{
clicks = aClick;
points = aPoint;
}

/**
Draw a triangle from three user-specified points
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
// draw circle around first point

if (clicks == 1)
{
double x = points[0].getX();
double y = points[0].getY();
final double RADIUS = 5;
Ellipse2D.Double smallCircle
= new Ellipse2D.Double(x - RADIUS, y - RADIUS,
2 * RADIUS, 2 * RADIUS);
g2.draw(smallCircle);
}

// draw line connecting first and second point

else if (clicks == 2)
{
g2.draw(new Line2D.Double(points[0], points[1]));
}

// draw triangle

else if (clicks >= 3)
{
g2.draw(new Line2D.Double(points[0], points[1]));
g2.draw(new Line2D.Double(points[1], points[2]));
g2.draw(new Line2D.Double(points[2], points[0]));
}
}
private int clicks;
private Point2D.Double[] points;
}
ExP10_7.java

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
/**
This program draws a user-specified triangle by clicking
on three points
*/
public class ExP10_7 extends Applet
{
public ExP10_7()
{
clicks = 0;
points = new Point2D.Double[MAX_CLICKS];
// add mouse click  listener
MouseClickListener listener = new MouseClickListener();
addMouseListener(listener);
}
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;

Triangle t = new Triangle(clicks, points);
t.draw(g2);
}

// implement the mouse press handler

class MouseClickListener extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
if (clicks >= MAX_CLICKS) return;
int mouseX = event.getX();
int mouseY = event.getY();
points[clicks] = new Point2D.Double(mouseX, mouseY);
clicks++;
repaint();
}
}
private int clicks;
private Point2D.Double[] points;
private final int MAX_CLICKS = 3;
}
P10.9
Line.java

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
/**
This class provides a method to draw a line
from two user-specified points
*/
public class Line
{
/**
Constructs a Line object
@param aClick the number of clicks the user pressed
@param aPoint the point that the user pressed on
*/
public Line(int aClick, Point2D.Double[] aPoint)
{
clicks = aClick;
points = aPoint;
}

/**
Draw a line from two user-specified points
and indicate the slope of the line at the midpoint
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
if (clicks == 1)
{
double x = points[0].getX();
double y = points[0].getY();
final double RADIUS = 5;
Ellipse2D.Double smallCircle
= new Ellipse2D.Double(x - RADIUS, y - RADIUS,
2 * RADIUS, 2 * RADIUS);
g2.draw(smallCircle);
}
else if (clicks >= 2)
{
g2.draw(new Line2D.Double(points[0], points[1]));
double x1 = points[0].getX();
double y1 = points[0].getY();
double x2 = points[1].getX();
double y2 = points[1].getY();
String message;
if (x1 == x2)
message = "vertical line";
else
{
double slope = (y1 - y2) / (x2 - x1);
// note that y coordinates grow downwards
message = "slope = " + slope;
}
double mx = (x1 + x2) / 2;
double my = (y1 + y2) / 2;
g2.drawString(message, (float)mx, (float)my);
}
}

private int clicks;
private Point2D.Double[] points;
}

 
Didn't find what you were looking for? Find more on how should i detect mouse click on JTextField Or get search suggestion and latest updates.




Tagged: