Reputation: 1148
I want to make a wizard using swing. I noticed that JOptionPane works just like a wizard, only the amount of input and the manner on which it is organized is limited.
Does anyone know how JOptionPane "waits" until the required input is given and the right button is pressed before returning the value at the end?
Does anyone know how to use JOptionPane so that the typical cardLayout of a wizard can be created?
Upvotes: 2
Views: 2591
Reputation: 57381
I think you need a modal JDialog
with CardLayout
to swap wizard's screens. When the JDialog
is invisible you can get the state from it and decide how to continue.
Upvotes: 4
Reputation: 205795
You can add arbitrary content to a JOptionPane
, as shown here. That content can be a panel having CardLayout
, as shown here. Given the JOptionPane.OK_CANCEL_OPTION
, JOptionPane
will wait until either button is clicked. If the result is JOptionPane.OK_OPTION
, you can examine the cards' content as required.
Upvotes: 5
Reputation: 31603
What do you mean with "wait"? Do mean "wait" in the sense of blocking the execution until a button is pressed? If so, there are many solutions, but one of the most easiest would be something like this:
while(block) {
Thread.sleep(500);
}
And your dialog sets block = false;
when an OK-button is pressed. There are more sophisticated solutions for that, this is just an example.
If you mean "wait" in the sense of all field must be filled, you could easily implement a listener for every field to enable a OK-button if the last field was edited.
You could have a look at the source code of JDialog
e.g. here. I think the blocking part is done by the method show()
from the super class Dialog
here.
My tip: Don't try to make a multi-page wizard on our own from scratch and don't try to block anything etc. This leads to more problems typically. Instead follow a tutorial like here. It explains how you can use a Dialog as a basis for a wizard.
Upvotes: 1