Diego Esquivel
Diego Esquivel

Reputation: 194

Why does my program not wait for me to click the button on a window to then open another window?

Here is my code I just want to know how can I make my program wait for me to click OK on the first window and then open the next window instead of opening them both at the same time. For example, I want it to pop the window that says hey and the program waits for me to click ok and then the window that says bye pop up and then the program waits for me to click ok (the button).

public class Interface extends JFrame implements ActionListener
{
JButton botonOk1 = new JButton("Ok");
JButton botonOk2 = new JButton("Ok");
JFrame frame1 = new JFrame();
JFrame frame2 = new JFrame();

public void createFirstInterface(String x){
    botonOk1.addActionListener(this);
    JLabel label = new JLabel(x);
    label.setFont(new Font("Arial", Font.PLAIN, 20));
    botonOk1.setBounds(270, 200, 150, 50);
    label.setBounds(200, 10, 250,150);
    frame1.setLayout(null);
    frame1.setMinimumSize(new Dimension(700,365));
    frame1.add(label);
    frame1.add(botonOk1);
    frame1.pack();
    frame1.setVisible(true);
}

public void createSecondInterface(String y){
    botonOk2.addActionListener(this);
    JLabel label = new JLabel(x);
    label.setFont(new Font("Arial", Font.PLAIN, 20));
    botonOk2.setBounds(270, 200, 150, 50);
    label.setBounds(200, 10, 250,150);
    frame2.setLayout(null);
    frame2.setMinimumSize(new Dimension(700,365));
    frame2.add(label);
    frame2.add(botonOk2);
    frame2.pack();
    frame2.setVisible(true);
}



public void call(){
    createFirstInterface("hey");
    createSecondInterface("bye");
    
}

public static void main(String[] args){
    Interface interface = new Interface();
    interface.call();
}

public void actionPerformed(ActionEvent e){
    Object check;
    check = new Object();
    check = e.getSource();
    
    if(check == botonOk1){
        frame1.setVisible(false);
    }
    
    if(check == botonOk2){
        frame2.setVisible(false);
    }
}
}

Upvotes: 0

Views: 43

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Or better still, make the windows modal dialogs such as a JDialog that has ModalityType set to APPLICATION_MODAL. This way the program flow waits for the dialog to be handled (to no longer display) before continuing.

For example:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;

import javax.swing.*;

public class Interface2 {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            Interface2 interface2 = new Interface2();
            interface2.call();
        });
    }

    public void call() {
        createInterface("Hey");
        createInterface("Bye");
        System.exit(0);
    }

    private void createInterface(String text) {
        JLabel label = new JLabel(text);
        label.setFont(label.getFont().deriveFont(Font.BOLD, 40f));
        JPanel centerPanel = new JPanel(new GridBagLayout());
        centerPanel.setPreferredSize(new Dimension(600, 300));
        centerPanel.add(label);

        JButton okBtn = new JButton("OK");
        okBtn.addActionListener(e -> {
            // get the window that holds this JButton
            Window win = SwingUtilities.getWindowAncestor((Component) e.getSource());
            // now close/dispose of it
            win.dispose();
        });
        JPanel btnPanel = new JPanel();
        btnPanel.add(okBtn);

        JDialog dialog = new JDialog(null, text, ModalityType.APPLICATION_MODAL);
        dialog.add(centerPanel, BorderLayout.CENTER);
        dialog.add(btnPanel, BorderLayout.PAGE_END);
        dialog.pack();
        dialog.setLocationByPlatform(true);
        dialog.setVisible(true);
    }
}

Note that modal dialogs are how JOptionPanes work to halt program flow until the dialog has been handled. Here we're just doing things more directly.

Upvotes: 1

Related Questions