Kelsey Abreu
Kelsey Abreu

Reputation: 1124

Two JFrames running at the same time

So my question is, how do I make it so that the main class doesn't run while another class is waiting for input from the user.

Maybe I'm thinking it the wrong way so please help me sort out my thoughts :)

Here is a little example/snippet.

Main Class is running a JFrame and then opens up another JFrame asking user for inputs the other JFrame is inside AddEditWindow class.

AddEditWindow temp = new AddEditWindow();

info[counter+1] = temp.newEditedInfo;

infoArray.add(info[counter+1]);

So pretty much, what I'm asking is, how do I make it so that it doesn't reach the 2nd line of code until the user finally finishes inputting everything.

The ActionListener/Event handler for AddEditWindow is just for a button.

So i'm waiting for a button to be clicked (so that the variable inside the AddEditWindow class is initialized) and then for the code to continue to

info[counter+1] = temp.newEditedInfo;

Hopefully I explained it well enough.

Don't know if I'm thinking about it wrong or what :|

Upvotes: 1

Views: 909

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347184

For simplicity sake, try something like...

int result = JOptionPane.showConfirmDialog(null, "Can I ask you a question", "Quesion", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (result) {
    case JOptionPane.YES_OPTION:
        //...
        break;
    case JOptionPane.NO_OPTION:
        //...
        break;
}

enter image description here

The JOptionPane is a very powerful component. If you supply a component instead of the string, it will display the component...

For example...

enter image description here

JPanel panel = new JPanel(new GridLayout(0, 2));
panel.add(new JLabel("Name"));
panel.add(new JLabel("Last"));
panel.add(new JTextField("Jason"));
panel.add(new JTextField("Cardanas"));
panel.add(new JLabel("Phone"));
panel.add(new JLabel("Email"));
panel.add(new JTextField("333"));
panel.add(new JTextField("X"));
panel.add(new JLabel("PID"));
panel.add(new JLabel("Donation"));
panel.add(new JTextField("X"));
panel.add(new JTextField("0"));
panel.add(new JLabel("Membership"));
panel.add(new JLabel("Points"));
panel.add(new JTextField("false"));
panel.add(new JTextField("0"));

int result = JOptionPane.showConfirmDialog(null, panel, "Quesion", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
switch (result) {
    case JOptionPane.OK_OPTION:
        //...
        break;
    case JOptionPane.CANCEL_OPTION:
        //...
        break;
}

Upvotes: 4

shan
shan

Reputation: 1202

I think you are asking about Modal dialog. Check this

Upvotes: 2

Related Questions