java multi-level scroll-able pop-up menus - java

I have this requirement where I need to show menus which can go multiple levels down. Now at each level there can be many objects in the menu which means I have to show a scroll bar in the menu.
1
1->1
1->2
2
I am able to write a custom class 'JScrollPopupMenu' which introduces a scroll bar to the menu. Now I can only control the first pop-up menu but for the next level menus I have no control over the pop-up menu being created
Example:
//this is the first pop-up menu
JPopupMenu scrollablePopupMenu = new JScrollPopupMenu();
JMenu menu = new JMenu("1");
scrollablePopupMenu.add(menu);
menu = new JMenu("2");
scrollablePopupMenu.add(menu);
//this is the child menu for "1", I have no control over //childMenu.getPopupMenu(), this is created internally inside JMenu
JMenu childMenu = new JMenu("1->1");
menu.add(childMenu);
childMenu = new JMenu("1->2");
menu.add(childMenu);
In the above example, the first level pop-up has a scroll bar but the next level pop-up does not. Is there any way how this can be achieved?

If you have that many menu items you should reconsider your GUI design.
1) A thumb-rule in usability says that you should not display more than 8 selections in a group.
2) Just because Windows have a scrollable menu in the Start-menu, doesn't make it a good example. In fact, Microsoft has left that in favour of searching instead, just be because a huge scrollable menu isn't user-friendly.

Related

How to add custom menu action and titlebar icon to JFrame

I have a custom JFrame. On the title bar I have an icon in the top left, a title, and then the standard minimize, maximize, and close buttons on the right.
When I click the icon in the title bar I get the standard options: Restore, Move, Size, Minimize, Maximize, Close.
How can I add my own menu option here? I'd like to add and "Always On Top" option here.
Additionally I'd like to add a button next to the max,min,close buttons on the title bar to allow uses to toggle the "Always On Top" state of the JFrame.
You could create your own customized Components. To do that, create a new class which extends JMenuBar for example and override the methods which fit your needs. Very often, for example, one wants to override paintComponent(Graphics).
That is not what you want, 'though. Customizing the JMenuBar wont work as you expect it to. The "JMenuBar" is another bar below the title bar. I am Mac user, but as far as my knowledge goes, it is not possible to customize the title bar, because that isn't handled by the JVM. The only thing that is modifiable without using native code is the Icon in the top left.
For further information on that, look at this question and the best answer there. This will help you a lot.
Your problem (adding a button at the top for toggling the alwaysOnTop status) is best solved by creating normal instances of a JMenuBar, a JMenu and a JMenuItem.
To then add that MenuBar to your Frame, use JFrame.setJMenuBar(JMenuBar). See also How to use Menus.
I hope this helps!

Java: How to check whether a JFrame's menu bar is displayed in the systems menubar or in the frame itself?

A JFrame in (From Swing) allows you to set a menu bar (Instance of MenuBar using JFrame.setMenuBar(mb);). This menu bar can appear in different locations depending on the system it's running on. If the operating system the appliation is running on has a menu bar on top of the screen, this menu bar set in the JFrame usually appears in this menu bar. If this is not supported, the menu bar will be shown in the top of the frame itself.
You can see the different behaviours on different systems in the example bellow:
This is an example of the code I use to set up the menu bar:
// Initialize a menu bar
MenuBar mb = new MenuBar();
// Initialize the menu and some menu items, add these to the menu bar
Menu m = new Menu("Menu 1");
MenuItem mi1 = new MenuItem("Menu Item 1");
MenuItem mi2 = new MenuItem("Menu Item 2");
MenuItem mi3 = new MenuItem("Menu Item 3");
m.add(mi1);
m.add(mi2);
m.addSeparator();
m.add(mi3);
mb.add(m);
// Set the menu bar
setMenuBar(mb);
My question is: How do I check whether the menu bar will be shown in the frame itself or in the system's menu bar (if supported)? It would be great if there's any possible way to check this without initializing and defining a menu bar.
Maybe this is impossible, if that is the case, is it possible to check for this same problem after a menu bar has been defined?
As shown here, apple.laf.useScreenMenuBar specifies a Mac OS feature that displays an existing JMenuBar atop the screen, where a Mac user expects to see it. Although it should be irrelevant to your application, the only time the menu bar is displayed there is when you put it there. A suitable cross-platform predicate might look like this:
if (System.getProperty("os.name").startsWith("Mac OS X")) {
System.setProperty("apple.laf.useScreenMenuBar", "true");
System.setProperty("apple.awt.graphics.UseQuartz", "true");
// etc.
}
is it possible to check for this same problem after a menu bar has been defined?
I only use windows so I don't know how it works on other OS.
I don't think you can find this information out until the frame is visible. Then you should be able to use:
Point location = menuBar.getLocatonOnScreen();
Then you can compare that location with the location of the frame to see if it is contained within the bounds of the frame.
if (frame.getBounds().contains(location))
....
The question is why do you need this information? Maybe we can suggest an alternate approach.

Get selected menu item & display the corresponding item on frame

I have 3 conditions (3 menu items in menu). In those 3 items, how to get one menu item selected as soon as the frame opens and displays the content of that item by default?
Later on if we select other menu items, then corresponding contents on the frame will be displayed as usual. I have written very lengthy code for this GUI, so unable to paste here. But got stuck up at this point.
Simply invoke the actionPerformed() method of your ActionListener after your GUI is fully constructed. This is particularly easy if you have implemented the Action interface, as shown in How to Use Actions.
Using this example, add the following line near setVisible() to simulate adding a few random nodes to the graph:
gp.control.random.actionPerformed(new ActionEvent(gp, 0, null));
To simulate clicking a button, this line simulates adding a selected node:
gp.control.defaultButton.doClick();
By default all menu items are not selected when they are created. To make a menu item selected before you show it in your application you should change the state of the model. For the JMenu items it's easy by setSelected(true) and setPopupMenuVisible(true). For the JMenuItem items you have to setArmed(true). You can return back to the default state in the actionPerformed.

Using visual swing JPopup menu in netbeans

i want to add a popup menu to my app but when i add it to my panel i see its disappear.
how i can edit jpopup menu visualy like other menus?
I think you're talking about using the matisse visual designer in NetBeans.
In which case the process is slightly different from creating menubar menus that you can just drag onto the screen.
The steps to get this to work are as follows
Drag a pop up menu on to the screen
Go to the Inspector Window (Window -> Navigating -> Inspector)
You will see an element there - jPopupMenu1
Right click jPopupMenu1 -> Add from Palette
This submenu will show you all the items that can be added to your pop-up menu
After you have selected the item you will see it listed as a child node of jPopupMenu1.
To change the order of your menu items, select an item, right click and select move up or move down.
Don't forget that although you've added the pop-up menu to the form you still need to register the pop-up menu with the component that you want it to activate for, the easiest way to do so is via SetComponentPopupMenu().

Java Swing JMenu Mnemonic

I have a menu that I created using JMenu. I want to assign a shortcut key Alt-F to this menu. I used setMnemonic('F') to do that, but the menu does not recognise the mnemonic.
What is the best way to troubleshoot or debug this problem ? I find that setting a break point does not help that much.
Thank you.
Code Snippet:
//higher up in variable declaration
/** Menus on the menu bar */
private JMenu uiFindMnu = new JMenu("Find");
...
//inside the constructor
// set mnemonic for the Find menu
uiFindMnu.setMnemonic('F');
Inside of a class constructor (extending JFrame):
JMenu helpmenu = new JMenu("File");
helpmenu.setMnemonic('F');
JMenuBar menubar = new JMenuBar();
menubar.add(helpmenu);
setJMenuBar(menubar);
That worked fine for me. You'll have to give some more details about your code in order for me to give a better answer. As far as troubleshooting SWING or any application GUI, one of the best recommendations I can give is to create the simplest possible scenario. I keep a bare-bones JFrame template around that I can throw simple code like this inside for testing. Once you know it works in the simplest scenario you can step back to your project and discover what other portion of your GUI is causing a conflict with this functionality.
Just out of curiosity, you don't happen to have a local variable called 'uiFindMnu' in your Constructor that is hiding your class variable, do you? I'd double check to make sure that the variable that you are calling setMnemonic() on is the one that is added to your MenuBar (and actually displayed).
Suffered with similar problem and realised that due to the setting of Look and feel after initialising the components caused the issue. Flipped the statements and it worked.
Posted a blog post here
Use setMnemonic(KeyEvent.VK_F);
I recommend you to read this about JMenus : Howto use Menus
Here is an extract of this article :
Menus support two kinds of keyboard alternatives: mnemonics and
accelerators. Mnemonics offer a way to use the keyboard to navigate
the menu hierarchy, increasing the accessibility of programs.
Accelerators, on the other hand, offer keyboard shortcuts to bypass
navigating the menu hierarchy. Mnemonics are for all users;
accelerators are for power users.
A mnemonic is a key that makes an already visible menu item be chosen.
For example, in MenuDemo the first menu has the mnemonic A, and its
second menu item has the mnemonic B. This means that, when you run
MenuDemo with the Java look and feel, pressing the Alt and A keys
makes the first menu appear. While the first menu is visible, pressing
the B key (with or without Alt) makes the second menu item be chosen.
A menu item generally displays its mnemonic by underlining the first
occurrence of the mnemonic character in the menu item's text, as the
following snapshot shows. B is the mnemonic character for this menu
item
You can specify a mnemonic either when constructing the menu item or
with the setMnemonic method. Here are examples of setting mnemonics
and accelerators:
//Setting the mnemonic when constructing a menu item:
menuItem = new JMenuItem("A text-only menu item",
KeyEvent.VK_T);
//Setting the mnemonic after creation time:
menuItem.setMnemonic(KeyEvent.VK_T);
As you can see, you set a mnemonic by specifying the KeyEvent constant
corresponding to the key the user should press.

Categories