This has been tested and works. The Look & Feel decides how to handle the mouse events on disabled menu items. Anyway, you can intercept the undesired events by using a custom MenuItem.
Simply use that code (copy/paste): public class CustomMenuItem extends JMenuItem { public CustomMenuItem(String text) { super(text); } public CustomMenuItem() { super(); } protected void processMouseEvent(MouseEvent e) { if (isEnabled()) super. ProcessMouseEvent(e); } } First, adapt the code to suit your needs (optional). Finally, replace any JMenuItem with a CustomMenuItem.
That's it!
Jmendeth ... Yes, this works perfect. Thanks a lot. – Brad Apr 20 at 15:48 @Brad you're welcome – jmendeth Apr 20 at 17:34 @jmendeth ... Is there a similar solution for JComboBox disabled items?
– Brad Apr 20 at 19:29 @Brad yes, just make your own JComboBox class and copy the processMouseEvent method into it. Then replace any JComboBox with your class. If you want, I can post an example.
– jmendeth Apr 21 at 15:47 @jmendeth ... I am not getting that ... Shouldn't the processMouseEvent be applied to the JComboBox items & not the JcomboBox itself? ... I'd be grateful if you write me an example. – Brad Apr 21 at 20:52.
Not sure how to prevent. But you can setVisible(false) to prevent it being displayed. Also if a user clicks on the disable menu no action will take place.
– Brad Mar 7 at 20:52 you can do saveMenuItem. SetVisible(false) – harshit Mar 10 at 5:54 But I want to make it visible and disabled! If I clicked a disabled item no action will occur, but the menu will close.
Try it. – Brad Mar 13 at 20:17.
When you are disabling JMenuItem, you should remove the ActionListener associated with that JMenuItem by using jMenuItem. RemoveActionListener() method. If you remove that that action will not call the listener and popup will not be disappeared.
I hope this will help to achieve your target.
Did you gave a try at this method: download.oracle.com/javase/6/docs/api/ja... "arm the menu item so it can be selected", which I guess would do the trick if set to false.
Thanks. I have tried it, but it is not working. – Brad Mar 13 at 20:04.
In short, you can do this, but you will have to write your own mouse listener, which may require a lot of copy&paste from the jdk source code, which is not a very good idea, and I'm not sure about what license restrictions it will put on your code. I would start digging from this method: javax.swing.plaf.basic.BasicMenuItemUI. Handler#mouseReleased which seems to be the entry point from where the menu handling mechanism hides the popup.
I would take a closer look at javax.swing.plaf.basic. BasicPopupMenuUI. MouseGrabber#stateChanged EDIT Developing answer by @Burhan Valikarimwala, try this apporach: remove all action listeners from the disabled JMenuItem and store them in some static temp structure (let's say a Map, List>), this way it will not hide the popup.
When you make the menu item enabled again, add all the listeners back. Make it into some util method and it will be seamless.
Too hard as you are saying. – Brad Mar 13 at 20:18 @Brad: see my edit. – Denis Tulskiy Mar 14 at 2:57.
The only solution I could come up with, for your problem of a click on disable JMenuItem causing it to hide is below: import java.awt. Color; import java.awt. Dimension; import java.awt.event.
ActionEvent; import java.awt.event. ActionListener; import java.awt.event. MouseAdapter; import java.awt.event.
MouseEvent; import java.awt.event. MouseListener; import javax.swing. JFrame; import javax.swing.
JMenuItem; import javax.swing. JPanel; import javax.swing. JPopupMenu; public class PopupMenuDisableNoCloseTest extends JPanel implements ActionListener { public static void main(String args) { PopupMenuDisableNoCloseTest p = new PopupMenuDisableNoCloseTest(); p.
SetPreferredSize(new Dimension(200, 300)); p. SetBackground(Color. GREEN); JPanel contentPane = new JPanel(); contentPane.
Add(p); final JFrame f = new JFrame(); final JPopupMenu popup = new JPopupMenu(); final JMenuItem menuItem1 = new JMenuItem("A popup menu item"); menuItem1. AddActionListener(p); menuItem1. AddMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { System.out.
Println(" menuItem1 mousePressed e.getPoint()=" + e.getPoint()); } @Override public void mouseReleased(MouseEvent e) { System.out. Println(" menuItem1 mouseReleased e.getPoint()=" + e.getPoint()); if(!menuItem1.isEnabled()) popup. SetVisible(true); } }); menuItem1.
SetEnabled(false); popup. Add(menuItem1); JMenuItem menuItem2 = new JMenuItem("Another popup menu item"); menuItem2. AddActionListener(p); popup.
Add(menuItem2); MouseListener popupListener = new PopupListener(popup); f. AddMouseListener(popupListener); f. SetContentPane(contentPane); f.
SetSize(800, 600); f. SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); f.
SetVisible(true); } @Override public void actionPerformed(ActionEvent e) { JMenuItem source = (JMenuItem) (e.getSource()); String s = "Action event detected. Event source: " + source.getText(); System.out. Println("s=" + s); } static class PopupListener extends MouseAdapter { JPopupMenu popup; PopupListener(JPopupMenu popupMenu) { popup = popupMenu; } @Override public void mousePressed(MouseEvent e) { maybeShowPopup(e); } @Override public void mouseReleased(MouseEvent e) { maybeShowPopup(e); } private void maybeShowPopup(MouseEvent e) { if(e.isPopupTrigger()) { popup.
Show(e.getComponent(), e.getX(), e.getY()); } } } } Basically the hiding happens when your release is inside the bounds of the JMenuItem, therefore we are checking if it is disabled then we show popup again. As by this time it is already decided that it will be hidden. I was trying calling super.
MouseRelease with a different MouseEvent pointing outside component and consuming the previous one but it helps nothing. Anyway this solution works. Enjoy, Boro.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.