2

Hello guys,

I just want to ask something if is it possible to remove JMenu.addSeparator() after it is being called? for example in my form there is a menu bar and inside the menu bar lets say there are three JmenuItems and each of it has JMenu.addSeparator(). What i want to do is, if a different user is log in I want to setVisible(false) one of JMenuItem because that particular user in not authorize to use that JMenuItem. The problem is when I setVisible(false) one of the JMenuItem the JMenu.addSeparator() still exist which kinda awkward to watch since there are no JMenuItem exist in the middle of two JMenu.addSeparator(). Hope you can help me with this problem. Thanks in advance

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
user3135677
  • 83
  • 1
  • 9
  • 1
    It might be easier to rebuild the menu from scratch per user – MadProgrammer Feb 13 '14 at 02:03
  • what do you mean rebuild the menu from scratch? – user3135677 Feb 13 '14 at 02:05
  • 2
    I mean just that. When a user is logged in, rather the "hiding" or "showing" menu items, you would rebuild the entire `JMenu` from scratch, only adding those menu items that the current user can actually use... – MadProgrammer Feb 13 '14 at 02:06
  • Actually that was my first thought, but i was thinking it might be easier if i just do the "hiding" or "showing" menu items that the current user can actually use. – user3135677 Feb 13 '14 at 02:11
  • So, then you would need to walk the `JMenu` check to see if two `JSeparator`s reside next to each other instead...I know, personally, what would be easier for me ;) – MadProgrammer Feb 13 '14 at 02:14
  • thats why i will follow your advice because i know that i still have a lot to learn and a long road to walk because i'm still a newb in this language, but anyway thanks for your advice hope your always there to help those who do not know everthing about this language..=) – user3135677 Feb 13 '14 at 02:24

2 Answers2

2

You have two possible solutions...

You Could...

Remove the contents of the menu and rebuilt it based on what the user can do...

menu.removeAll();
// Add menu items back in...
// Personally, I'd have some method that could return back all
// the JMenuItems that could appear on this menu based on the
// the user...

This would be my preferred solution...

You Could...

Hide/show the menu items based on what the current user can actually do and then remove all the JSeparators that appear next to each other, for example...

Component last = null;
for (Component comp : menu.getComponents()) {
    if (comp instanceof JSeparator && last instanceof JSeparator) {
        menu.remove(comp);
    } else {
        last = comp;
    }
}

Personally, I know which I would prefer and generally, which would produce consistent results...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • thats one of my problem since the structure of my code is not consistent which i can hardly produce consistent results. Because i'm not following the proper build up of codes which is very frustrating. – user3135677 Feb 13 '14 at 02:30
0

I ran into a situation where I had to remove the separator from an existing menu. (Old code and wasn't allowed to refactor the whole mess.)

So I used the idea of MadProgrammer's 2nd solution - but rewrote it to actually work.

        Component last = null;
        for (int idx = 0; (idx < m_fileMenu.getMenuComponentCount()); idx++) {
            Component comp = m_fileMenu.getMenuComponent(idx);
            if (comp instanceof JPopupMenu.Separator && last instanceof JPopupMenu.Separator) {
                m_fileMenu.remove(comp);
                idx--;
            } else {
                last = comp;
            }
        }
CasaDelGato
  • 1,263
  • 1
  • 12
  • 29