I'm building simple Swing Java application, it contains JFrame and JPanel. My JPanel's constructor needs 1 parameter to pass.I want to give my user's possibility to enter this variable. So now my app just starts and I need to pass this parameter in code. My goal is: before my JPanel (and maybe JFrame) starts there appears small window with question "Select a number" and select list with numbers from 1 to 10, and when user will choose one and press the button, this value should be passed into myJPanel's constructor.
How to achieve that?
Code of JFrame:
public class MainWindow extends JFrame {
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow frame = new MainWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainWindow() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 542, 455);
myJPanel panel1 = new JPanel(4); // HERE
add(panel1);
}
}
EDIT
Is there any more elegant way to do it? : ] I dont want to convert to string, and then to int.
And, how to close whole app if user clicks Cancel button?
Object[] possibleSizes = { 3, 4, 5, 6, 7 };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose a number", "Uwaga!",
JOptionPane.INFORMATION_MESSAGE, null,
possibleSizes, possibleSizes[0]);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 542, 455);
MyJPanel panel1 = new MyJPanel(Integer.parseInt(selectedValue.toString()); // HERE
add(panel1); 1);