Search code examples
javaswingjframejdialogmodality

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);
}
}

Solution

  • 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.