I have two jbutton in a jpanel and when you click the first button you see a msg box saying "good morning" and when you click the second you see a "good night" msg box . But when i click on the first button and not closing the msg box first i can not click the second button to see the message.
Can anyone help me?
thanks.
This would suggest two basic things, you are using either a JOptionPane or modal JDialog to display your messages.
In either case, a modal dialog blocks the parent window until it is dismissed. You need to make the dialog modeless instead.
Take a look at How to Make Dialogs
Related
I wish to create a dialog Pop Up Box where the details of an Item ,the image next to it and a "Yes" or "No" Button Options below them. Specifically, I am trying to show the details of a DVD and ask the user if he wishes to rent it.
Here's the picture if that helps(sorry I couldn't find an image online):
Then I wish to create an additional pop up promting the user for information if he chooses the "Yes" option.
My question is how to create this? I didn't find any answers in the JOptionPane documentation and tutorials.
You should use a JOptionPane (as you said.)
Basically, you want to have JOptionPane create a dialog for you. For a yes-or-no question, you would use a confirm dialog. So you would start with this:
int choice = JOptionPane.showConfirmDialog(this,
contents, "dialog-title", JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
Now, to explain the code:
The first argument is the frame that the dialog will show up on. I'm assuming you're calling this from an existing frame's code, so I put this there.
Next, you'll see a variable called contents. It can be any object, but here you want it to be a JPanel. Basically, you create a single panel containing everything you want above the buttons and pass it in as the message. This will put that panel above the two buttons (which JOptionPane will create for you).
The next argument is just the title of the dialog.
YES_NO_OPTION gives you the "yes" and "no" buttons instead of "ok" and "cancel".
I used PLAIN_MESSAGE because that doesn't put an icon on the dialog.
Finally, there is a return value, which I stored in choice. It will be equal to either JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, or JOptionPane.CLOSE_OPTION. From there, if the user clicks "yes", you can make the next dialog show up and create it in the same way.
i have displyed this JDialog , and have passed an Object which is a JPanel on it , thus my JDialog displays my JPanel on it when Invoked as required.
and on this JPanel I have a JButton, on pressing i want some operations to happen which i have written in it's ActionListener and in the end i have to dispose that JDialog, but i have no clue how to do it !!
This s my JDialog Statement and help me with HOW TO EVEN REMOVE AN ICON from JDialog as even after keeping the ICON PARAMETER NULL it displays the ICON.
JOptionPane.showOptionDialog(null, "SELECT ITEM AND THEN EDIT THE DETAILS.",
"EDIT ITEM DETAILS", int1, int2 , null, objEditMorePane, null);
It sounds like you want to make it a JOptionPane.PLAIN_MESSAGE. That is what you need to put instead of whatever int2 is. I was going to link you to the tutorial but Space Pope already did that. You don't need to create a custom dialog to change the default icon, just change the message type to a plain message. The tutorial covers all this stuff.
You'll need to keep a reference to the dialog if you want to close it yourself. See the Oracle tutorial on custom Dialogs. The constructor you're using also puts in an icon by default; if you make your own dialog, you can control that part too.
JOptionPane closes its dialog when its value property changes. So, you can obtain the parent JOptionPane and set its value to close the window:
JOptionPane optionPane = (JOptionPane)
SwingUtilities.getAncestorOfClass(JOptionPane.class, button);
optionPane.setValue(JOptionPane.CLOSED_OPTION);
If the window wasn't created by JOptionPane, you can use the getTopLevelAncestor method of JComponent to obtain the parent window:
Window window = (Window) button.getTopLevelAncestor();
window.dispose();
I have written an application that has one main window and multiple dialogs, however one of these dialogs does not close when the user clicks the 'X' button at the top right corner. There is an OK button in the dialog which closes it correctly, so it's not a huge issue. Can anyone suggest why it's not working?
Maybe you are missing such a line: setDefaultCloseOperation(HIDE_ON_CLOSE); in the dialog class?
I have a piece of code in my program where in I need to display an error message. Code:
String ErrorMsg=" Error to be Diplayed ";
JOptionPane.showMessageDialog(null, ErrorMsg, "Failure", JOptionPane.ERROR_MESSAGE);
Note: Default frame is used.
The message is successfully displayed, but before acknowledging by pressing "OK" button if I try any other successful flow the message box control is lost and message box won't be on foreground blocking even the successive flows.
I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden. How to do that?
If you want JOptionPane to behave as it would in a full-fledged GUI, then first create a full-fledged Swing GUI. Forget using "default" frames or whatever you're using (the console perhaps). You are desiring GUI behavior, and so to get this you must create a GUI by displaying your application in a JFrame and have the JFrame launch the JOptionPane.
Pass reference to the parent frame instead of the null (first param).
I want the Message Box to be on the foreground always until the user presses "OK" button, rather losing focus and getting hidden.
Use a JFrame and setAlwaysOnTop(true). You will need to display your own message and button.
A JOptionPane uses a JDialog behind the scenes. A JDialog does not support this property.
Edit:
To get the icon used by the option pane you can use:
Icon icon = UIManager.getIcon("OptionPane.errorIcon");
For a list of the other icons see: UIManager Defaults.
The below code is enough to make a JoptionPane message with default frame to be set on top .
JDialog dialog = new JOptionPane("ErrorMsg",JOptionPane.ERROR_MESSAGE,JOptionPane.DEFAULT_OPTION).createDialog(" Failure");
dialog.setAlwaysOnTop(true);
dialog.setVisible(true);
dialog.dispose();
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.