1

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);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
pawel
  • 5,976
  • 15
  • 46
  • 68
  • 1
    Maybe http://docs.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html – tckmn Nov 08 '12 at 02:55
  • @PicklishDoorknob nice, thanks - but I still can't find how to close whole application when user will click "Cancel" : / – pawel Nov 08 '12 at 03:06
  • What does @PicklishDoorknob: have to do with closing the whole application? Have you tested the result returned from the JOptionPane if cancel is pressed? – Hovercraft Full Of Eels Nov 08 '12 at 03:08
  • @HovercraftFullOfEels I mean showInputDialog actually – tckmn Nov 08 '12 at 03:12
  • @HovercraftFullOfEels yes, i've tested it - it has compilation errors then. Could you guys check 1st post edit? – pawel Nov 08 '12 at 03:18
  • You make it difficult to help if you don't tell us what the compilation errors are. – Hovercraft Full Of Eels Nov 08 '12 at 03:19
  • 1
    Ah, I see. You're trying to make up constructors for JPanel, which makes no sense. Why don't you instead call the constructor of the class you made? Also, please adhere to Java's naming convention including giving classes names that start with a capital letter, lest you make it hard for volunteers here to understand your code. – Hovercraft Full Of Eels Nov 08 '12 at 03:20
  • You're right, but when I'm trying to call `MyJPanel panel1 = new MyJPanel(Integer.parseInt(selectedValue.toString()); // HERE add(panel1); 1);` - i still have the same problem – pawel Nov 08 '12 at 03:27
  • You're still not posting the compilation error! – Hovercraft Full Of Eels Nov 08 '12 at 03:34

1 Answers1

3

You're probably looking for something like JOptionPane.showInputDialog.

This is very simple, but will allow you to collect information from the user, which is returned as a String

For more information check out How to make Dialogs, JOptionPane

You can get really complex results using the JOptionPane, check out accept only a single digit in java for an example

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366