Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the difference between mouseClicked and mousePressed.  RSS feed

 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I add a mouseListener to a table.
mytable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
JTable table =(JTable) me.getSource();
table.clearSelection();
Point p = me.getPoint();
int row = table.rowAtPoint(p);
System.out.println(row + " is the row you clicked!");
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mouseClicked it the click u make (ie. the press and the release).
mousePressed is just the pressing action. u dont need to realese for it to work.
in my experience, i found using the pressed works much better then the clicked on all platforms.
when i can i use the pressed instead of the clicked, but u can use whatever is easier for u.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

mouseClicked is when the mouse button has been pressed and released.

mousePressed is when the mouse button has been pressed.

This description can be found in the MouseListener Interface documentation from the Java 2 Platform, Standard Edition, v 1.4.0, API Specification. The MouseAdapter class documentation also includes a similar description.

Good Luck,
-Dirk Schreckmann
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry, I didn't finish yet.
The mouseListener doesn't work very well untill I change mouseClicked to mousePressed. I am wondering why? Is there anybody can tell me the difference between mouseClicked and mousePressed?
Another thing I notice is that even if mytable didn't get the mouseClicked event. The row I clicked is still selected. I try to select the row only when the table get the mouseClicked event
myTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
JTable table =(JTable) me.getSource();
table.clearSelection();
Point p = me.getPoint();
int row = table.rowAtPoint(p);

if (row != -1)
try {
table.changeSelection(row, 0, false, false); System.out.println(row + " is the row you clicked!");
} catch (Exception e) {
}
});
It didn't work. So when user clicks on the table, what is the order of highlight a row and listener to the mouselistener?
Thanks in advance!
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like i mentioned in my post, the mouseClicked isnt that good on some platofmrs with some mice.
i found the mouseClicked like u did, BUGGY and problematic. if doesnt always catches the clicks, u need sometimes to double click for it to work.
my suggestion then, as i mentioned, is to use mousePressed instead.
 
Renee Zhang
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy and Dirk:
Thank you so much for your reply!
The reason I use mouseClicked is that I thought if I used mousePressed I couldn't get ClickCount. now I know I can. So my problem is solved.
I still feel weird why sometimes table can't capture the mouseClicked event. Is that becase I clicked between 2 rows? Or the table thinks that it's a mousePressed event? There are so many things in the JSwing are so confusing.
Thanks again!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!