0

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);
    }
}
}
3
  • 2
    Call createSecondInterface("bye"); from within actionPerformed.
    – luk2302
    Jun 12, 2019 at 15:07
  • Read your code. Where do you wrote to open the first frame? How does the mecasim differnt from the second one? Where could you wrote a different mecanism that would react on first click?
    – jhamon
    Jun 12, 2019 at 15:08
  • @luk2302: your answer is valid, why delete it? Jun 12, 2019 at 16:04

1 Answer 1

1

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.

3
  • Ok thanks, i dont know what JDialog is or Modality Type but if it makes java wait for me to click the button to then keep going with the program ill research into it, i hope it works, thanks. Jun 12, 2019 at 15:23
  • @DiegoEsquivel: please see example above Jun 12, 2019 at 15:28
  • Oh ok thanks, ill try this and yeah actually i was trying to look for what does showMessageDialog and showInputDialog methods had in them but i couldnt find anything so if you say this is kind of what they have then im sure this will help, so thank you. I mean i dont understand some of it but its ok ill try to to make it work with my program. Jun 12, 2019 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.