Question about JPopupMenu behavior. I would like the JPopupMenu not to loose focus when it comes up. Also when JPopupMenu is in focus, the user should be able to click/update other parts of the Applet.
Is this possible? Reason is that this JPopupMenu is displaying some codes and it needs to be kept open for reference.
This is what I have now but as soon as the user clicks on the main Applet JPopupMenu looses focus:
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.show(component, x, y);
thanks for your help.
Don't use a JPopupMenu for this purpose.
You can use a non-modal JDialog as the "popup". Make the dialog undecorated and the border will not be painted. Make sure you specify the owner when you create the dialog. You can use the SwingUtilities.windowForComponent(..) method to get the Window to be used as the owner.
"The exact gesture that should bring up a popup menu varies by look and feel." See Bringing Up a Popup Menu for details about doing this in a platform-independent way. Also, consider extending JApplet instead of Applet.
Related
When two JFrames are enabled, I want the user to be able to use only one in the top (think of that like an error pop up on your screen, when you can't press anything but the popup itself). I am aware of the class JInternalFrame and I chose not to use it for my program. Thanks in advance :)
Use JDialog, you can set your main frame as the JDialog's parent frame, so that whenever your main frame and JDialog will display, you will be able to click only the JDialog, not your main frame.
You want a modal behaviour, then. I think you can try using JDialog instead of JFrame, something like:
JDialog dialog = new JDialog(parentFrame, title, true); //parameters: owner, title and modal
dialog.getContentPane().add(somePanel);
dialog.pack();
dialog.setVisible(true);
You can read more about it here: http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html
I'm new to Java and Swing. I created a jframe and I added a menubar and MenuItem in it.
On clicking a menu item, a jdialog should open. Now the jdialog has a jtextfield in it and a jlabel. Now the problem for me is 'when dialog is opened for first time, the textfield is empty and thats correct. Now i close the jdialog and i open it again but now instead of getting an empty textfield in jdialog, i get the data entered previously' which is not what should happen as the jdialogs 'default close operation' property is set to 'dispose'. but that is not happening for me...
I dont know what i'm doing wrong. I have never tried applet/swing before in any other way (consider this as my first demo learning programme)
Second Image here
The JTextField is retaining it's value because it isn't being affected by the JDialog closing, instead it is being hidden as it's parent (the JDialog) is invisible
Setting the dialog to dispose isn't re-initialising the child components, so they keep their values. Some additional information on this behaviour is available here:
JDialog setVisible(false) vs dispose()
JDialog
One way you can prevent / control this is by "informing" the dialog to wipe the textfield as it is closing by adding a WindowEvent and providing the necessary functionality in the windowClosing() method
Netbeans gui-builder will generate this for you with the following:
Right click Dialog
Events
Window
WindowClosing
Providing:
private void jDialog1WindowClosing(java.awt.event.WindowEvent evt) {
// TODO add your handling code here:
}
In which you can add: textfield.setText(""); to clear the textfield
Another approach is to create your own dialog and setting up the components in the constructor. As creating a new instance will contain the components with their default values, effectively resetting it
I am running Windows. When you run an application on Windows, you get a button task bar where you can click it to maximize and minimize it. Is it possible to create a JFrame without this or some other component that has the functionality of a JFrame but without adding it to the task bar.
Use a JDialog instead of a JFrame. On a JDialog, you can set the 'modal' property, which means no 'upper bar' or anything is displayed.
Do make sure the JDialog has no parent frame or anything though: a modal JDialog will block the GUI of any parent GUI component. But if you just use it as your main component there is no problem :)
When I let popup a JPopupMenu without passing the "invoker component", the menu doesn't work: submenu's don't open and isn't getting repainted. But when I create a completely useless JFrame with a JLabel inside, and I pass the JLabal as invoker, it works correctly...
Any suggestions, how to avoid creating a useless frame. And my application really hasn't any frames open, it just has to popup a simple menu.
JPopupMenu.show(null, xOnTheScreen, yOnTheScreen); // Doesn't work
JPopupMenu.show(aStupidJLabelInAStupidJFrame, x, y); // Works
Thanks
Take a look at JPopupMenu source code and you'll see why you have to set an invoker.
Showing a popup menu without any existing component would be very bad usability, in the same league as popup windows from a browser.
Why can't you use JComponent#setComponentPopupMenu, or add a mouse listener to the component in which you want to show popup menu?
I'm developing the Java Swing application. I'm quite new to Java, so got some questions. I have a modal window with some set of controls (text fields, buttons etc).
I want to handle click on the button in the parent window. I think the most efficient and accurate way is first to handle it in modal window, then raise some another event from the model form and handle it in the parent form.
Is this approach right and what are the best practices on doing that?
Thanks for your help!
In general, the dialog that contains the button should handle the button click.
However, maybe you can use a JOptionPane. It is designed to return which button was clicked and then you can do custom processing based on the clicked button. Check out the section from the Swing tutorial on How to Make Dialogs for some examples. Also not that you can add a panel to an option pane. In this case you may find the Dialog Focus tip usefull.
I suppose what you want is for the action (or an action listener) of a button in the parent window to process a mouse click on a button (or anything) in the modal dialog.
There are infinite ways to do that. You can pass the action to the modal dialog, pass the button and call doClick(), pass an implementation of an interface that can redirect mouse click (or anything), etc.
Or if instead you want to click the actual button in the parent window when the modal dialog is up, look up the definition of modal.