I have a problem. You see, I want to add my own items to the window menu bar of my application, not add items to a JMenuBar. Like for example, look at the menu bar of the browser you are using. That type of menu bar. I know Java is run in a VM so you can't access too many things outside of the JVM, but is there any possible way that can be done? Whenever I search "Java menu bar" it comes up with just JMenuBar stuff and nothing about creating a window menu bar. Thanks!
You can make the JMenuBar look however you want. You just need to configure the menu bar and its menus appropriately.
Related
I'm currently working on a school assignment where I need to create an app for visually impaired users. I would like to use Java with JavaFx for GUI.
Preferred element for visually impaired users is menu bar, since it can be easily navigated by keyboard. Menu bar in JavaFx is a MenuBar class object, that has Menu class attribute, which has MenuItem class attributes. Windows Narrator (neither the NVDA screen reader) works with this menu bar out of the box. It won't read the labels at all. It just says "Menu" and that's it.
Menu and MenuItem do not have accessibleText attribute which stores the text to be read by the screen reader software. Is there a way to make this menu bar screen reader compatible?
TL;DR: Is there a way to create menu bar that works with Windows Narrator in JavaFx?
For future reference: I've managed to solve this. The issue was I didn't have Java Access Bridge enabled (https://docs.oracle.com/javase/7/docs/technotes/guides/access/enable_and_test.html). Enabling it made the menu bar work without any other changes.
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!
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.
I am developing a Java application using Swing. I am using menu bar in my application. Whenever I click a menu item I need to open a new panel/frame within the window containing menu options.
To be precise I don't want to open a new window on clicking a menu item. What is the possible solution to this problem?
Thanks in advance.
If you want to put windows inside of other windows, you should look at JInternalFrame:
http://download.oracle.com/javase/tutorial/uiswing/components/internalframe.html
If you don't want to open a new window when clicking a menu item, CardLayout is a good choice. Also, don't overlook the convenience of using actions in your menus.
My problem is this
I want to show a popup menu to a menu item in java, this i can easily achieve,
but when i am showing the popup menu the parent menu clears,
behaviour i want is parent menu also should be visible while showing popup menu
May I first ask "why"? It seems like very uncommon, thus bad usability. There's a good reason why you don't see this in any main stream application. If you want submenu, use submenu like trashgod's sample.
You can add a PopupMenuListener to your parent menu's popup menu, break at popupMenuWillBecomeInvisible and see the call stack. In JDK6, It comes from BasicMenuItemUI#doClick calling MenuSelectionManager#clearSelectedPath.
So if you really have a good reason to surprise user, supply your own menu item UI.
I'm looking at two demos, PopupMenuDemo and GraphPanel, and I don't see any clearing. PopupMenuDemo shows a hierarchical submenu, while GraphPanel shows a hierarchical context menu. Do you have an example in which "the parent menu clears."
Addendum: Here's what I see for PopupMenuDemo. Neither demo appears to invoke setVisible() on the menu.