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.
Related
Hy everyone,
I was just learning some new things with Java GUI, and I got stuck with a problem. This is what it looks like:
I want a button that adds the data from the second JPanel in to the first one.But not only once. I want it to go to the first JPanel, everytime it is pressed.
So, the button should create a new JTextField in the first JPanel, below their respective categories "name" and "age" everytime it is pressed. It means that I have to modify the "Y" field everytime, so all the new JTextFields created by the button "add data" dont get pilled up.
I don't know how to make the button "add data" Works, the other things, I know how to do. I know how to make the button creates a JTextField, with the data I want to store, only once (using getText() and setText() ), but not how to create new JTetFields with diferente "y" field everytime it is pressed.
Thank you for the help.
When you click on the button to add people you should not display a new JPanel. Instead you should display a modal JDialog. A JDialog is just like a frame but generally is only displayed for a specific function and then closed.
On this dialog you will probably want two buttons: 1) Save Data and 2) Close. This will allow you to enter information for multiple people before closing the dialog.
To display the "person" information you should probably be using a JTable. A table allows you to display data in a row/column format. See the section from the Swing tutorial on How to Use Tables for basic information to get you stated. Note, in this cause you will want to use a DefaultTableModel for your table and then you can just use the addRow(...) method to save the "person" information.
When you create the class for the dialog to get the "person" information you will want to pass the DefaultTableModel to this class as a parameter. Then when you click the "Save Data" button you get the information from the two text fields and update the TableModel. The table will be updated automatically.
So your first step is to create the main frame with the JTable with the two columns. Then you create a simple "Add Person" button. This button will then add hard coded data to the table each time it is clicked by using the addRow(...) method. Once you get this working then you change the logic of the button to display the dialog and then you can enter the "person" information on the dialog and save it.
Some of the constructors provided by Dialog API's DialogDescriptor accept JPanel as the first parameter. Is it possible to have a button inside this panel that triggers the Dialog closing as well as specify a return value?
I want to make such a "resolving conflict" dialog, just like how Ms Windows offers Copy/Discard/Copy Both options when we copy files from one directory but there are files with the same name in the destination directory. Note that pressing one of three buttons will close the dialog, but actually none of them is listed as closing options; the closing options itself consist of "Skip" and "Cancel".
Maybe you can try think the other way: making a panel manipulating the dialog outside of it introduces unnecessary dependency. Why not make the dialog pass in which button is clicked when closing and have the following logic done in the panel? You purpose is to do something when "Skip" is clicked and something else when "Cancel" is clicked, right? So let the dialog tell your panel what's clicked instead of let the panel dispose the dialog would be less coupling.
Im trying to make a java program that will popup/notify me to stop what I am currently doing and do something. more like a reminder. My question is how will I make a pop up in java.
I found this docs, but dont know how to implement for the parent component.
JOptionPane.showMessageDialog(??,"this is a modal dialog.");
The answer depends, do you have a parent component (like a JFrame or other component) or are you simply display the JOptionPane independently?
If you are simply displaying the JOptionPane independently, you can simply pass it null.
If you are displaying the JOptionPane as part of a large application, with windows and other components, you can simply pass it a reference of what ever component you want the JOptionPane to displayed relative to, such as the window or most accessible container/component
The parent argument (if supplied) simply allows the dialog to act as a modal (blocking) dialog for the window which contains the supplied component. This requires the user to have to dismiss the dialog before they can continue to interact with the parent window/component
Take a closer look at How to Make Dialogs for more details
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 want to know how i can do a messageBox from three input dialog ..
Like this:
JOptionPane.showInputMessageDialog("Enter your FirstName");
JOptionPane.showInputMessageDialog("Enter your MiddleName");
JOptionPane.showInputMessageDialog("Enter your LastName");
But I want one message has a three input boxes.
Build a JPanel (supose it's named inputPanel) with the three JtextFields to input and then do this:
if (JOptionPane.YES_OPTION == JOptionPane.showconfirmDialog(
parentComponent, inputPanel, "Enter your data", JOptionPane.YES_NO_OPTION) {
// retrieve data from the JTextFields and do things
} else {
// User close the dialog, do things... or not
}
You can't do that with JOptionPane. Create a JDialog and add three JTextField's to it instead. A JDialog will block the caller when you call setVisible(true), so it's easy to create a dialog that waits for user input before it returns.
showInputMessageDialog and its brethren are simple ways to whip up a simple "standard" dialog. For more complicated dialogs, I'm pretty sure you'll have to subclass JDialog or such.
As Telcontar has suggested you can add Swing components (like a JPanel) to an option pane. So it is easy to take advantage of the automatic creation of buttons rather than do it from scratch by building your own JDialog.
However, there is one small problem. The focus will be on the first button, not on the first component of your panel. To get around this problem you can try the solution presented in Dialog Focus.
You can find the standard Java Tutorial Example here:
Click Here to Open the example java file
The example has only one text box, but the example is clear enough for you to extend it.