This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JMenuItem Accelerators does not work with me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenuItem Accelerators does not work with me" Watch "JMenuItem Accelerators does not work with me" New topic
Author

JMenuItem Accelerators does not work with me

Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
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.


Hesham
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Feb 26, 2007
Posts: 66
Thanks Rob for the tips ... I will read that now to stop bugging you
Hesham Gneady
Ranch Hand

Joined: Feb 26, 2007
Posts: 66
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

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

Joined: Feb 26, 2007
Posts: 66
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

Joined: Feb 26, 2007
Posts: 66
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JMenuItem Accelerators does not work with me
 
Similar Threads
coding a texteditor: want to open a separate option window
Adding Submenu to JMenuItem.
JMenuItem.setMargin
How to get
MenuBar and Toolbar problem