user1337192
user1337192

Reputation: 101

JFrame to behave as JDialog?

I've a simple JDialog with JOptionPane and it works fine. But I want to use JFrame and create more complex window but I want it to act as JDialog, it means I need to halt code when JFrame or JDialog opens as next steps depends on what is chosen in window.

Here is my Class, is there anyway to run it so that it halts my code

public class MyFrame extends JDialog{
private static final long serialVersionUID = 1L;
public MyFrame() throws IOException {
    setSize(600, 450);
    dialogInit();
    setTitle("Travel");
    setVisible(true);
    URL url = this.getClass().getResource("/images/travel1.png");
    BufferedImage bf = ImageIO.read(url);
    this.setContentPane(new backImage(bf));
    JButton b = new JButton("Send");
    JCheckBox tf=new JCheckBox("Country");
    b.setBounds(318, 143, 98, 27);
    tf.setBounds(235, 104, 150, 27);
    add(b);
    add(tf);
}
}

Upvotes: 2

Views: 1169

Answers (3)

rob
rob

Reputation: 6247

In your code, you're still making a JDialog since MyFrame extends JDialog, which is what you probably want instead of a JFrame. What you need to do in addition is to make it modal.

Making your dialog via an appropriate constructor or by using setModal(true) will block input to other windows of your program and halt execution in the caller until the dialog is closed.

Upvotes: 0

Brian
Brian

Reputation: 17299

What you want is for your dialog to be modal. Use Dialog.setModal(boolean):

public MyFrame() throws IOException {
    setModal(true);

Then when you call setVisible on the dialog from the code that constructed it, the code will wait until the dialog is closed.

Upvotes: 3

Russell Zahniser
Russell Zahniser

Reputation: 16354

You can have your frame manually run the event pump until it closes. See this question.

Upvotes: 0

Related Questions