Disable Menu Items in JideSplitButton

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Disable Menu Items in JideSplitButton

Postby judodan » Mon Sep 17, 2012 12:33 pm

Is there a way to disable menu items in a JideSplitButton? I want to disable only some of the menu items when the menu is opened. I couldn't find any example code to do this.

Thanks!
judodan
 
Posts: 5
Joined: Thu Sep 13, 2012 9:18 am

Re: Disable Menu Items in JideSplitButton

Postby JIDE Support » Mon Sep 17, 2012 1:55 pm

Please invoke Action#setEnabled() to control the behavior of the menu items. The sample code is posted below.
Code: Select all
        final JideSplitButton button = new JideSplitButton(name);
        AbstractAction action0 = new AbstractAction("Sample Menu Item 1") {
            public void actionPerformed(ActionEvent e) {
            }
        };
        action0.setEnabled(false);
        button.add(action0);
        AbstractAction action1 = new AbstractAction("Sample Menu Item 2") {
            public void actionPerformed(ActionEvent e) {
            }
        };
        button.add(action1);

Thanks,
---------
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am

Re: Disable Menu Items in JideSplitButton

Postby judodan » Mon Sep 17, 2012 2:21 pm

It's not actually that simple. Because I'm keeping a list of fields (to sort on), every time I open the popup, I want to enable/disable the selection based on what is visible in the listbox (should not be able to add the same field twice). From your hint, I was able to resolve this by adding a PopupListener. Code is included below in case someone else has same issue:

Code: Select all
       jAddSortButton.getPopupMenu().addPopupMenuListener(new PopupMenuListener() {
           public void popupMenuWillBecomeVisible(PopupMenuEvent evt) {
               JPopupMenu menu = (JPopupMenu) evt.getSource();
               DefaultListModel model = (DefaultListModel)jSortList.getModel();
               for (Component child : menu.getComponents()) {
                   if (child instanceof JMenuItem) {
                       JMenuItem item = (JMenuItem)child;
                       boolean enabled = !model.contains(item.getText());
                       item.setEnabled(enabled);
                   }
               }
           }
           public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) {
               
           }
           public void popupMenuCanceled(PopupMenuEvent evt) {
               
           }
       });



Works great now!
judodan
 
Posts: 5
Joined: Thu Sep 13, 2012 9:18 am

Re: Disable Menu Items in JideSplitButton

Postby JIDE Support » Mon Sep 17, 2012 2:30 pm

Glad to know that you had worked it out. And thanks for your sharing.

Thanks,
--
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 9 guests

cron