Showing popup menu to menu item - java

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.

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!

Where in Java Swing Source is the implementation of the Enter Keypress on a menu?

I am mucking around with a hierarchical menu trying to make it scrollable. Yes, I know about Menu Scroller at the Java Tips Weblog, but it doesn't quite do what I want, so I've been mucking about with a stripped down version of it it and I'm not quite getting it to work.
Basically I want a JMenu with too many items to display on which the user can press the up and down arrow keys to scroll the menu. I have gotten tanatalizingly close to what I want but I have come to a hurdle which I can best describe this way:
When [ENTER] is pressed while a popup menu has focus, default behavior is to do the action associated with the selected item and dispose of the menu. If the menu is nested, popups above it in the hierarchy also close (become invisible). Where is this behavior coded? I've looked all over JMenu, JPopupMenu, JMenuItem, AbstractButton and I don't see what I am looking for. Where is the Swing source code that executes this common behavior?
If I knew the answer to that, I might understand why my implementation isn't working. I can do the action, but the menu and its parents won't disappear. I can make the menu disappear by setVisible(false) of course, but I can't walk the containment hierarchy to find the parent menus and make THEM disappear.
I can do the action, but the menu and its parents won't disappear.
I think you can use:
MenuSelectionManager.defaultManager().clearSelectedPath()
I'm not 100% certain for menus, but I know for JTextComponents that all of the keystrokes (copy, paste, enter, move forward by words/sentences/lines, deleting, etc.) are implemented via the InputMap and ActionMap. JTextcomponents also use Keymaps, but I'm pretty sure those are specific to text components.

How do I fire click() event whenever i click on any item in the popupmenu in java?

I want to add a window on clicking the menu item of a right click popupmenu in Java swing(seems like a simple task...but not getting)
I got it to partly work by
adding a custom menuitem in popupmenu
adding a window on right click
But this only partly works. Now I when I right click anywhere a window pops up, but the selected menu item in the popupmenu does not pop up the window.
How can I get window on clicking my menu item? or is there an easier way to do this in java?
I know there are others ways of doing this (in code), but I would prefer to use Java swing
Has anyone ever done this before?
Thanks for your help
please to read Oracle tutorial about How to Use Menus
How to Use Swing Actions and / or with How to Write an Action Listener
a few examples about JPopup /JPopupMenu

NetBeans remove automatically generated action listener in design view

I'm using NetBeans design view. I added an action listener to a JMenuItem, which I want to remove. I seem unable to do it, because the generated code is in the grey area and the IDE won't let me edit it. I can't remove it through the properties window either. What do I do?
You can make changes to guarded code see here http://wiki.netbeans.org/FaqFormEditingGuardedBlocks
But I don't think you should.
Click on the JMenuItem, go to Properties pane.
In Properties pane go to Events tab and remove the action listener from there. If you give nothing there, it will show "<none>" as the event listener.
Now the action listener function will not be called, although the code will still be there.
But, if those dead lines of code are bothering you, then you can either comment them, or remove the JMenuItem and add again. This will remove the code completely.
Select Design view, then find your JMenuItem in the Navigator pane. It is located by default on the left. If it is not present, select in the menu:
Window => Navigating => Navigator
or, as alternative click Ctrl+7.
When you find your JMenuItem in the Navigator pane, select it, right click and choose Delete.
UPDATE
Of course, by doing the above actions you'll get rid of JMenuItem itself.
If you wanted to get rid of action listener itself, then choose <none> for the desired action as #nitgeek suggested.

Jtextpane click to create popup menu effect on selection not as intended

I have a JTextPane sitting in a JFrame, with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
I want to give the JTextPane a "Word-like" popup behavior. By that I mean, if you right click outside of your current text selection, the caret will reposition to where you right clicked, with menu options that affect a text selection (such as cut, copy, or bold) disabled. If you right click within your current text selection, the popup will appear with options that effect text selection enabled, the text selection will persist, and the caret will not move.
The problem is I cannot seem to find where I can put the code that handles the selection change. I tried:
Using the "PopupMenuWillBecomeVisible" event which is triggered before a popup becomes visible. The event passed into this method does not contain any mouse event information so there is no way for me to use viewtomodel to find out how to modify the selection. I could use MouseInfo but that seems dubious at best.
Using MousePressed/MouseReleased events in the JTextPane or JFrame. Apparently, neither of these events are invoked when a popup menu is triggered. In fact, I still can't determine what the parent component of my popup menu is. (I did read that in windows "MouseReleased" is the popup trigger, while in other systems "MousePressed" is the trigger. I tried both and neither worked).
So, I guess the problem is that I can't seem to find a place to put code where it would be called before the popup menu becomes visible, but has awareness of the mouseEvent that triggered the popup menu. I must be missing something here.
with a popup menu that is assigned to the JTextPane through the JTextPane.setComponentPopupMenu method.
You can use the older approach of displaying the popup based on your own custom MouseListener.
See the section from the Swing tutorial on Bringing Up a Popup Menu. Now you have access to the MouseEvent so you can convert that point to a point in the Document so you know where the click was made, on selected or unselected text.

Categories