This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JToggleButton - mouseClicked problem  RSS feed

 
Amit Rosner
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I found a bug in JToggleButton and I want to know if it is a more general bug, and how can it be bypassed.
I have some JToggleButtons grouped in a ButtonGroup. On each of them I registered an action listener and a mouse listener.
By clicking back and forth ,I noticed that occasionally, the JToggleButton does not receive a mouseClicked event - although the mousePressed and mouseReleased are received as expected.
I am running on W2K using j2sdk1.4, and I noticed that happen also in j2sdk 1.3, but now it happens much often.
In the following example code below, the output messages:
Pressed
Released
Clicked
...
Pressed
Released
Clicked
Will soon become:
Pressed
Released
Pressed
Released

It is easy to reproduce the bug:
1.Create some JToggleButtons, group them with a ButtonGroup.
2.Register an action listener and a mouse listener.
3.Start clicking on each other.
4.Soon enough, the mouseClicked method will not be called periodically.
Example Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JApplet
{
public void init()
{
JToggleButton button1 = new JToggleButton("Beep1");
JToggleButton button2 = new JToggleButton("Beep2");
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
JPanel contentPane = new JPanel();
contentPane.add(button1);
contentPane.add(button2);
button1.addMouseListener
(
new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("Clicked");
}
public void mousePressed(MouseEvent e)
{
System.out.println("Pressed");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Released");
}
}
);
button2.addMouseListener
(
new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("Clicked");
}
public void mousePressed(MouseEvent e)
{
System.out.println("Pressed");
}
public void mouseReleased(MouseEvent e)
{
System.out.println("Released");
}
}
);
button1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Toolkit.getDefaultToolkit().beep();
}
}
);
button2.addActionListener (
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Toolkit.getDefaultToolkit().beep();
}
}
);
setContentPane(contentPane);
}
}
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't duplicate the problem. The code you supplied works fine with the appletviewer on my machine (I'm using JDK 1.3.1_02 on Win98SE). The only thing that I can possibly think of is that the events are getting out of order possibly due to system lag. I'm not completely sure that there is any guarantee of events ariving in the exact order that they were received.
Sorry I can't help any more with this
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!