Open popupMenu only on mouse-click Swing - java

I have a question related with JPopupMenu that maybe someone can help me.
As far as I can see, a JPopupMenu opens when you press a JMenu (is this is in a JMenuBar) or when you hover over a JMenu (if this is in a JPopupMenu).
So, I have a JMenuBar containing a JMenu (let's call it 'File'). When I press 'File', a JPopupMenu open, which contains a JMenuItem and another JMenu (let's call it 'Properties').
Is there any way I can apply the same behaviour to the Properties JMenu as I have in the
File JMenu: only opens the JPopup when the user click it and not hover over it?
I know this is trying to avoid the normal behavior for the JMenu, but any of you have an idea if this is possible?

This is something very specific and I dont know if there are many people doing this. :)
Anyway, I leave a solution for this in case someone needs it.
First is important to notice that these changes are done in the UI classes. Both BasicMenuItemUI and BasicMenuUI have a inner class Handler that take care about the mouse events. The inner class Handler in the BasicMenuUI extends the inner class Handler in the BasicMenuItemUI and because they private and anonymous (respectively) is hard to change the code.
But, in both cases, there are a protected Handler which points to the Handlers I talked in the previous paragraph.
So, as a solution I extended those protected handlers in my own MyOwnBasicMenuUI and MyOwnBasicMenuItemUI implementations and I applied my own behavior.
Now I have a JMenu which only opens when the user clicks it, and remains open till the user clicks somewhere else.
If someone wants more details, I can provide them.
Thanks

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!

Replacing contents in a frame using the optionpane-GUI

updating a panel
I am designing a program for my assignment using java.
I have made a log on box where user enters name and password and clicks on submit or register. Once clicked, an optionpane is brought
upEX.(JOptionPane.showMessageDialog(null, "Account registered", "Account",
JOptionPane.INFORMATION_MESSAGE);)
I was wondering how can I add an action listener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Another question:
My java file looks something like this
class MainFrame extends JFrame {
public MainFrame() {
// All the log in panels buttons are here, etc
}
}
I was wondering if it would be efficient to add the new box details in the same area or make a new class?
I was wondering how can I add an actionlistener to the OK button in the option pane that would remove all contents (username,pass,2btns) and replaces it with something else? I found this link: updating a panel which basically just says to use frame.remove and then add.
Don't. Allow the dialog to close, ascertain the action the user took, show another dialog...
Better yet, create you own JPanel, use a CardLayout, add all your views to it and navigate between them. Place this on an instance of a JFrame...
See How to Use CardLayout for more details
I was wondering if it would be efficent to add the new box details in the same area or make a new class?
It would depend. A Class should be a self contained unit of work, focused on accomplishing it's designed task...if your main class is doing more work then it should, then yes, separate the logic into separate class

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.

JButton needs to be clicked twice after JDialog

I've been struggling with some problem while creating my app based on Swing. I have a main JFrame which consists of:
JMenu
JPanel containing a JButton and a JLabel
JButton is linked with ActionListener. Clicking JMenu (MenuListener) brings up a JDialog with some form. The problem is, when the JDialog is closed (it doesn't make difference whether I do it with dispose() or rather showVisible(false)) I need to click the JButton two times before it triggers for the first time. From now it normally works with one click.
Every time the JDialog is in front, the problem appears.
PS. The JDialog is set to be modal, with JFrame as parent.
It sounds like a focus issue.
The first click restores focus to the app and the second clicks the button. Typically, I have seen this when the JDialog has the wrong parent and focus can not be returned.
Thank you for your answers.
I have considered posting some code, but it involves 4 classes so will be quite long.
I have also tried things with focus before, but nothing helped. What is interesting: if I display the JDialog by new myDialog.showVisible(true) it behaves like I've described. But if I use construction like this:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JDialog.setVisible(true);
}
});
it closes normally and parent frame doesn't need to be clicked before responding, but on the other hand the displayed Dialog needs so. Additonally, what I do not understand, after opening the Dialog cursor is placed in the text field and I can write normally, but to click some button on it I must click once on the Dialog, only second and next clicks behave like I want to.
PS. Closing the dialog like in the second included example changes nothing.

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