This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JMenuItem Accelerators does not work with me

 
Hesham Gneady
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I am trying to click "Ctrl + A" to fire a menuItem click event. I know its simple but i don't understand why its not working. I have attached a small Jar sample with my source code inside it to test it, and here is the part of my code defining the accelerator :

Thanks in advance.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you open the File menu, do you see a CTRL+A behind the menu item's text?

Of course, why didn't I see this immediately... JMenuItems should not use MouseListeners for handling them being activated, but ActionListeners.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes .. You are absolutely right ... It worked fine now with ActionListeners.
I've been working with Java for more than 2 years now, and the same problem !! I'm always using MouseListeners. I need to understand ActionListeners better to use it.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to understand both, as both have different purposes. ActionListener is used with JMenuItem, JButton, JCheckBox, but also JTextField when you press enter. MouseListener is available for every component, and can be used for many, many purposes.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for the tips ... I will read that now to stop bugging you
 
Hesham Gneady
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have mostly changed all my listeners in my code from MouseListeners to ActionListeners. Everything now is going smooth and fine.

The last case i have is a JTable with a column of CheckBoxes inside it. I was using MouseListeners to detect any enable/disable event for any of the CheckBoxes inside that column and it was working fine except if i click on a CheckBox + drag with the mouse a very small distance then leave the mouse ... This leads to disabling or enabling the CheckBox but no event occurs.

I have checked the JTable listeners to replace the MouseListener. I have tried the TableModelListener but no event occurs, example :

Finally, i have tried ListSelectionListener and it worked fine, but the event occurred twice, example :

Can you please explain to me why TableModelListener didn't work and why ListSelectionListener was executed twice ?

Much appreciated ...
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that you are using the default mechanism for displaying check boxes in your JTable (i.e. having a column with getColumnClass returning Boolean.class) and a table model that correctly fires events when its setValueAt method is called, then a TableModelListener should work.

For the ListSelectionListener, check out the return of e.getValueIsAdjusting().
 
Hesham Gneady
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i think i am using a normal JTable ... Nothing special.

I have tried getValueIsAdjusting() like this :

And the text is printed once, that's fine ... But when i enable/disable another CheckBox, nothing happens. The event occurs the first click only !
I am still checking if something in my code contradicts with that.
 
Hesham Gneady
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay ... It's now working. It worked when i used the TableModelListener(). It was a problem in my code, i was recreating the table model again somewhere else in my code, so it was overwriting the created listener. Now i fixed it.

Thanks a lot Rob.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic