Creating Wizard Swing

你离开我真会死。 提交于 2019-12-08 02:07:36

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/10375293/creating-wizard-swing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!