Generate a double click mouse event in Java Swing - java

I am trying to generate a double click mouse event on the EDT as follows:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
component.dispatchEvent(new MouseEvent(
component,
MouseEvent.MOUSE_CLICKED,
System.currentTimeMillis(),
InputEvent.BUTTON1_MASK,
x, y,
2, // click count
false
));
}
});
This does not seem to dispatch a double click event, even though I am setting the click count to 2.
Any suggestions or examples?

Considering:
final JButton clickTwiceButton = new JButton();
final JButton fireEventButton = new JButton();
Listeners:
clickTwiceButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if (evt.getClickCount() == 2) {
JOptionPane.showMessageDialog(null, "Double clicked!");
}
}
});
fireEventButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
// Invoking later for no reason, just to simulate your code
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
clickTwiceButton.dispatchEvent(new MouseEvent(
fireEventButton,
MouseEvent.MOUSE_CLICKED,
1,
MouseEvent.BUTTON1,
0, 0,
2,
false
));
}
});
}
});
When I click fireEventButton the MouseEvent gets correctly dispatched to clickTwiceButton, and the dialog appears as expected.
So, as #Andrew pointed out, the problem seems to be that either you are firing the event to the wrong component or that something is not right with the registered MouseListener / MouseAdapter code.
Use component.getMouseListeners() to check for your component Listeners and debug the code that handles its events.

The method is very simple. You should get the time of the first click and the time of the second click, then you can do a condition in between.
Method code as below:
private boolean state=false;
private long first_pressed;
JButton btnAdd = new JButton("add");
btnAdd.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
if(state==false) {
first_pressed=e.getWhen();
state=true;
}
if(first_pressed!=e.getWhen())
{
JOptionPane.showConfirmDialog(null,"doubel click","Click",JOptionPane.YES_NO_OPTION);
state=false;
}
}
});

public class TestMouseListener implements MouseListener {
private boolean leftClick;
private int clickCount;
private boolean doubleClick;
private boolean tripleClick;
public void mouseClicked(MouseEvent evt) {
if (evt.getButton()==MouseEvent.BUTTON1){
leftClick = true; clickCount = 0;
if(evt.getClickCount() == 2) doubleClick=true;
if(evt.getClickCount() == 3){
doubleClick = false;
tripleClick = true;
}
Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
Timer timer = new Timer(timerinterval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(doubleClick){
System.out.println("double click.");
clickCount++;
if(clickCount == 2){
doubleClick(); //your doubleClick method
clickCount=0;
doubleClick = false;
leftClick = false;
}
}else if (tripleClick) {
System.out.println("Triple Click.");
clickCount++;
if(clickCount == 3) {
tripleClick(); //your tripleClick method
clickCount=0;
tripleClick = false;
leftClick = false;
}
} else if(leftClick) {
System.out.println("single click.");
leftClick = false;
}
}
});
timer.setRepeats(false);
timer.start();
if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
}
}
public static void main(String[] argv) throws Exception {
JTextField component = new JTextField();
component.addMouseListener(new TestMouseListener());
JFrame f = new JFrame();
f.add(component);
f.setSize(300, 300);
f.setVisible(true);
component.addMouseListener(new TestMouseListener());
}
}

Related

Determining the sequence of two clicks a user makes

public class GlassActionListener{
JButton first;
JButton second;
#Override
public void actionPerformed(ActionEvent e) {
if(first == null)
{
first = (JButton) e.getSource();
}
else
{
second = (JButton) e.getSource();
// do something
// clean up
first = null;
second = null;
}
}
}
public class GUIControl {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGUI();
}
});
}
public static void showGUI() {
JFrame frame = new JFrame("MyFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JButton glass1 = new JButton("glass1");
JButton glass2 = new JButton("glass2");
JButton glass3 = new JButton("glass3");
frame.getContentPane().add(glass1);
frame.getContentPane().add(glass2);
frame.getContentPane().add(glass3);
frame.pack();
frame.setVisible(true);
glass1.addActionListener(??);
glass2.addActionListener(??);
glass3.addActionListener(??);
// I want to store the first two clicks in these variables
from = "glassX";
to = "glassY";
puzzle.move(from, to);
}
The user will spill soda between the glasses. The first JButton the user clicks out of the three available determines the "from" initial glass and the second JButton the user clicks out of the three available determines the "to" destination glass.
How can I determine the specific two JButtons that the user clicked?
Everytime the user clicks two out of the three JButtons I want to store the string associated with those two JButtons in a "from" and "to" variable.
Is there any way to do this?
make an ActionListener and use JButton#addActionListener to add it to all three buttons. The event has a source field you can use.
ActionListener listener = new ActionListener() {
JButton first;
JButton second;
#Override
public void actionPerformed(ActionEvent e) {
if(first == null)
{
first = (JButton) e.getSource();
}
else
{
second = (JButton) e.getSource();
// do something
// clean up
first = null;
second = null;
}
}
};
glass1.addActionListener(listener);
glass2.addActionListener(listener);
glass3.addActionListener(listener);
Handling the logic inside the listener is a little dirty, but that's the idea.
firstbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do something here first button is clicked
}
} );
you can add listeners to buttons like this.and use a class variable to track how many times the buttons are clicked.
eg:
public class GUIControl {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGUI();
}
});
}
public static void showGUI() {
JFrame frame = new JFrame("MyFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JButton glass1 = new JButton("glass1");
JButton glass2 = new JButton("glass2");
JButton glass3 = new JButton("glass3");
String from = "";
String to = "";
int click_count = 0;
glass1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(click_count ==0)
{
from = "glassX";
click_count = 1;
}else if(click_count ==1)
{
from = "glassY";
click_count = 0;
}
}
} );
glass2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(click_count ==0)
{
from = "glassX";
click_count = 1;
}else if(click_count ==1)
{
from = "glassY";
click_count = 0;
}
}
} );
glass3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(click_count ==0)
{
from = "glassX";
click_count = 1;
}else if(click_count ==1)
{
from = "glassY";
click_count = 0;
}
}
} );
frame.getContentPane().add(glass1);
frame.getContentPane().add(glass2);
frame.getContentPane().add(glass3);
frame.pack();
frame.setVisible(true);
// I want to store the first two clicks in these variables
}

Final local variable error

I'm basically making a tic tac toe game, with AI and all, and my system is to draw the buttons, and have a Boolean assigned to each button, where it is assigned to true if it's taken by an X, or false if it's empty. It draws out to the correct size and layout and all but in my action listeners it gives me the error:
Edit: removed the finals, still giving the error
"The final local variable cannot be assigned since it is defined in an
enclosing type."
when I change the boolean to true after assigning the button text to 'x'.
package myClass;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class mainClass {
public static void main(String[] args) {
//create window
JFrame mainWindow = new JFrame("Tic Tac Toe");
//properties of mainWindow
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWindow.setSize(600, 600); // buttons will fill entirely, 200x200 buttons
mainWindow.setLayout(new GridLayout(3, 3));
//create buttons
JButton topLeft = new JButton("");
JButton topMid = new JButton("");
JButton topRight = new JButton("");
JButton midLeft = new JButton("");
JButton midMid = new JButton("");
JButton midRight = new JButton("");
JButton botLeft = new JButton("");
JButton botMid = new JButton("");
JButton botRight = new JButton("");
//checker for if the button already has a character
boolean tmid = false;
boolean tright = false;
boolean mleft = false;
boolean mmid = false;
boolean mright = false;
boolean bleft = false;
boolean bmid = false;
boolean bright = false;
//button properties
Dimension buttonSize = new Dimension(200,200);
topLeft.setSize(buttonSize);
topMid.setSize(buttonSize);
topRight.setSize(buttonSize);
midLeft.setSize(buttonSize);
midMid.setSize(buttonSize);
midRight.setSize(buttonSize);
botLeft.setSize(buttonSize);
botMid.setSize(buttonSize);
botRight.setSize(buttonSize);
//Action listener
topLeft.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(tleft == false){
topLeft.setText("X");
tleft = true;
}
}
});
topMid.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(tmid == false){
topMid.setText("X");
tmid = true;
}
}
});
topRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(tright == false){
topRight.setText("X");
tright = true;
}
}
});
midLeft.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(mleft == false){
midLeft.setText("X");
mleft = true;
}
}
});
midMid.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(mmid == false){
midMid.setText("X");
mmid = true;
}
}
});
midRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(mright == false){
midRight.setText("X");
mright = true;
}
}
});
botLeft.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(bleft == false){
botLeft.setText("X");
bleft = true;
}
}
});
botMid.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(bmid == false){
botMid.setText("X");
bmid = true;
}
}
});
botRight.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(bright == false){
botRight.setText("X");
bright = true;
}
}
});
//draw components
mainWindow.add(topLeft);
mainWindow.add(topMid);
mainWindow.add(topRight);
mainWindow.add(midLeft);
mainWindow.add(midMid);
mainWindow.add(midRight);
mainWindow.add(botLeft);
mainWindow.add(botMid);
mainWindow.add(botRight);
mainWindow.setVisible(true); // draw it
}
}
There are two problems. One, you cannot modify final variables. Two, you have to use final variables to have scope within the listeners. This code needs serious refactoring. However, here is a quick solution that requires minimal intervention.
Remove all of the boolean tleft; boolean tmid; ... variables
Add this enum outside of the main() method definition
enum ESide {
tleft, tmid, tright,
mleft, mmid, mright,
bleft, bmid, bright,
};
Add this definition at the appropriate spot
// checker for if the button already has a character
final Map<ESide, Boolean> board = new HashMap<>();
for (ESide side : ESide.values()) {
board.put(side, false);
}
Replace the listeners with the following, adjusting the tleft as appropriate. However, seriously consider making a method that returns the mouse adapter.
public void mouseClicked(java.awt.event.MouseEvent evt)
{
if (board.get(ESide.tleft).booleanValue() == false) {
topLeft.setText("X");
board.put(ESide.tleft, true);
}
}
The error you are receiving will be eliminated.

Mouse event goes through the component

I have added a simple program to illustrate.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimpleInternalFrame extends Frame
{
private static final long serialVersionUID = 1L;
static JLayeredPane desktop;
JInternalFrame internalFrame;
public SimpleInternalFrame()
{
super("Internal Frame Demo");
setSize(500, 400);
Panel p = new Panel();
add(p, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
desktop = new JDesktopPane();
desktop.setOpaque(true);
add(desktop, BorderLayout.CENTER);
}
public static void main(String args[])
{
SimpleInternalFrame sif = new SimpleInternalFrame();
sif.setVisible(true);
final JInternalFrame internalFrame = new JInternalFrame("Internal Frame 1", true, true, true, true);
internalFrame.setBounds(50, 50, 300, 200);
desktop.add(internalFrame, new Integer(1));
JTextField tf = new JTextField();
tf.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
System.out.println("Text Field " + evt.getClickCount());
}
});
internalFrame.add(tf);
internalFrame.setVisible(true);
final JInternalFrame internalFrame2 = new JInternalFrame("Internal Frame 1", true, true, true, true);
internalFrame2.setBounds(50, 50, 200, 100);
desktop.add(internalFrame2, new Integer(1));
JButton jb = new JButton("click me");
jb.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if (evt.getClickCount() == 1)
{
System.out.println("Button " + evt.getClickCount());
internalFrame2.setVisible(false);
}
}
});
internalFrame2.add(jb);
internalFrame2.setVisible(true);
}
}
When running the code it will open a two internal frames.One has button.One has text field.Button will close the first Internal frame for single click.
Double click the button.It will show click count 2 as in text field.
This is the problem we have currently in the project.Thing is second frame dose not has text field in actual project.It has click-able item that work in double click event.
This is the problem we have currently.Please help.
tf.addMouseListener(new MouseAdapter() {
int cc;
public void mouseClicked(MouseEvent evt) {
int ccount = evt.getClickCount();
if(ccount == 1 || ccount == cc+1) {
cc = ccount;
System.out.println("Text Field " + evt.getClickCount());
}
}
});
This will work more than once.
Another possibility is to use components with overriden processMouseEvent():
public class SimpleInternalFrame extends Frame {
...
private MouseEvent lastMouseEvent;
public boolean checkEvent(MouseEvent e) {
if(lastMouseEvent != null) {
if(lastMouseEvent.getSource() != e.getSource()) {
if(e.getClickCount() != 1) {
return false;
}
}
}
lastMouseEvent = e;
return true;
}
class MTextField extends JTextField {
protected void processMouseEvent(MouseEvent e) {
if (checkEvent(e)) {
super.processMouseEvent(e);
}
}
}
class MButton extends JButton {
protected void processMouseEvent(MouseEvent e) {
if (checkEvent(e)) {
super.processMouseEvent(e);
}
}
}
public JTextField createText() {
return new MTextField();
}
public MButton createButton() {
return new MButton();
}
} //end of SimpleInternalFrame
create components:
final JTextField tf = sif.createText();
tf.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
System.out.println("Text Field " + evt.getClickCount());
}
});
JButton jb = sif.createButton();
jb.setText("click me");
jb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 1) {
internalFrame2.setVisible(false);
}
}
});
You can use a static boolean called myTurn initialized to false and then in the textfield mouseClicked:
tf.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent evt)
{
if (evt.getClickCount() >= 2 && !myTurn) {
myTurn = true;
evt.consume();
} else {
myTurn = true; //Initial one click on me
System.out.println("Text Field " + evt.getClickCount());
evt.consume();
}
}
});
This solves your problem in this basic case, but I don't know if it will in your application. The problem is that if you click the TextField before the Button, you can experience the same problem. However, if that isn't possible, this will solve it.
While searching on internet and reading some documents i was able get a solution.
int timerinterval = (int) Toolkit.getDefaultToolkit().
getDesktopProperty("awt.multiClickInterval");
final Timer timer = new Timer(timerinterval, new ActionListener()
{
public void actionPerformed(ActionEvent acEvt)
{
internalFrame2.setVisible(false);
}
});
jb.addMouseListener(new MouseAdapter()
{
public void mouseClicked(final MouseEvent evt)
{
System.out.println("Button " + evt.getClickCount());
timer.setRepeats(false);
timer.start();
if (evt.getClickCount() > 1)
{
timer.restart();
}
}
});
One draw back of this solution is if user do a single click he need to wait
timerinterval unnecessarily.Any one has better solution please post.

java binding the keys correctly for a game

I am trying to move a rectangle up,down,left,right when up,down,left,right keys are pressed. I am puzzled why the key listeners no longer listen when the game is started..i.e when gameloop is started. Before starting the game or pressing "start" the listener works i.e it prints "up pressed" System.out.println("up pressed"); but when the game is started by clicking the start button then the listener no longer work. Why is this so? Appreciate any help!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GameLoopTest extends JFrame implements ActionListener
{
private GamePanel gamePanel = new GamePanel();
private JButton startButton = new JButton("Start");
private JButton quitButton = new JButton("Quit");
private JButton pauseButton = new JButton("Pause");
private boolean running = false;
private boolean paused = false;
private int fps = 60;
private int frameCount = 0;
public GameLoopTest()
{
super("Fixed Timestep Game Loop Test");
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
JPanel p = new JPanel();
p.setLayout(new GridLayout(1,2));
p.add(startButton);
p.add(pauseButton);
p.add(quitButton);
cp.add(gamePanel, BorderLayout.CENTER);
cp.add(p, BorderLayout.SOUTH);
setSize(1000, 700);
startButton.addActionListener(this);
quitButton.addActionListener(this);
pauseButton.addActionListener(this);
Action handle_up_action_pressed = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.up_pressed= true;
System.out.println("up pressed");
}
};
Action handle_up_action_released = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.up_pressed= false;
}
};
Action handle_down_action_pressed = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.down_pressed= true;
}
};
Action handle_down_action_released = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.down_pressed= false;
}
};
Action handle_left_action_pressed = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.left_pressed= true;
}
};
Action handle_left_action_released = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.left_pressed= false;
}
};
Action handle_right_action_pressed = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.right_pressed= true;
}
};
Action handle_right_action_released = new AbstractAction(){
public void actionPerformed(ActionEvent e){
gamePanel.right_pressed= false;
}
};
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), "handle_up_pressed");
gamePanel.getActionMap().put("handle_up_pressed", handle_up_action_pressed);
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, true), "handle_up_released");
gamePanel.getActionMap().put("handle_up_released", handle_up_action_released);
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "handle_down_pressed");
gamePanel.getActionMap().put("handle_down_pressed", handle_down_action_pressed);
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "handle_down_released");
gamePanel.getActionMap().put("handle_down_released", handle_down_action_released);
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), "handle_left_pressed");
gamePanel.getActionMap().put("handle_left_pressed", handle_left_action_pressed);
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "handle_left_released");
gamePanel.getActionMap().put("handle_left_released", handle_left_action_released);
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "handle_right_pressed");
gamePanel.getActionMap().put("handle_right_pressed", handle_right_action_pressed);
gamePanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), "handle_right_released");
gamePanel.getActionMap().put("handle_right_released", handle_right_action_released);
}
public static void main(String[] args)
{
GameLoopTest glt = new GameLoopTest();
glt.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Object s = e.getSource();
if (s == startButton)
{
running = !running;
if (running)
{
startButton.setText("Stop");
runGameLoop();
}
else
{
startButton.setText("Start");
}
}
else if (s == pauseButton)
{
paused = !paused;
if (paused)
{
pauseButton.setText("Unpause");
}
else
{
pauseButton.setText("Pause");
}
}
else if (s == quitButton)
{
System.exit(0);
}
}
//Starts a new thread and runs the game loop in it.
public void runGameLoop()
{
Thread loop = new Thread()
{
public void run()
{
gameLoop();
}
};
loop.start();
}

Distinguish between a single click and a double click in Java

I search the forum and see this codes:
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.out.println(" and it's a double click!");
wasDoubleClick = true;
} else {
Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty(
"awt.multiClickInterval");
timer = new Timer(timerinterval.intValue(), new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (wasDoubleClick) {
wasDoubleClick = false; // reset flag
} else {
System.out.println(" and it's a simple click!");
}
}
});
timer.setRepeats(false);
timer.start();
}
}
but the code runs incorrectly(Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!"). Can anybody show me why? or can you give me some better ways to do this?
Thank you!
Sometime it prints out " and it's a single click!" 2 times . It should print out " and it's a double click!").
That is normal. A double click only happens if you click twice within the specified time interval. So sometimes if you don't click fast enough you will get two single clicks in a row.
Integer timerinterval = (Integer) Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
The above line of code determines how fast the double click must be.
For what its worth here is some code I have used to do the same thing. Don't know if its any better or worse than the code you have:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ClickListener extends MouseAdapter implements ActionListener
{
private final static int clickInterval = (Integer)Toolkit.getDefaultToolkit().
getDesktopProperty("awt.multiClickInterval");
MouseEvent lastEvent;
Timer timer;
public ClickListener()
{
this(clickInterval);
}
public ClickListener(int delay)
{
timer = new Timer( delay, this);
}
public void mouseClicked (MouseEvent e)
{
if (e.getClickCount() > 2) return;
lastEvent = e;
if (timer.isRunning())
{
timer.stop();
doubleClick( lastEvent );
}
else
{
timer.restart();
}
}
public void actionPerformed(ActionEvent e)
{
timer.stop();
singleClick( lastEvent );
}
public void singleClick(MouseEvent e) {}
public void doubleClick(MouseEvent e) {}
public static void main(String[] args)
{
JFrame frame = new JFrame( "Double Click Test" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.addMouseListener( new ClickListener()
{
public void singleClick(MouseEvent e)
{
System.out.println("single");
}
public void doubleClick(MouseEvent e)
{
System.out.println("double");
}
});
frame.setSize(200, 200);
frame.setVisible(true);
}
}
public void mouseClicked(MouseEvent evt) {
if (evt.getButton()==MouseEvent.BUTTON1){
leftClick = true; clickCount = 0;
if(evt.getClickCount() == 2) doubleClick=true;
Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");
timer = new Timer(timerinterval, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(doubleClick){
System.out.println("double click.");
sb = new StringBuffer();
sb.append("Double Click");
clickCount++;
if(clickCount == 2){
clickCount=0;
doubleClick = false;
}
} else {
sb = new StringBuffer();
sb.append("Left Mouse");
System.out.println("single click.");
}
}
});
timer.setRepeats(false);
timer.start();
if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
}

Categories