Make main window wait until popup has been closed [Java]

Accname

2D-Graphics enthusiast
Reaction score
1,462
Hi guys.
Hopefully an easy question.
I am working with java here, using lwjgl to make an openGL window.
In the case of any kind of error (exception being thrown or illegal input or anything) i want to open a popup window displaying an error message.
I think this will be easier and more consumer friendly then having the game just crash to the desktop and making a cryptic error log. (i make the error log anyways)

So for the popup i dont want to use openGL as well, wanna make it a little bit easier on me and just use a jframe.

I made the following sub class of jframe:
Code:
    private static class Error_Popup extends JFrame {
        private static final long serialVersionUID = 4013003667450751116L;
        private JPanel contentPane;
       
        public Error_Popup(String title, String message, int x, int y) {
            setType(Type.POPUP);
            setTitle(title);
            setAlwaysOnTop(true);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            setMinimumSize(new Dimension(512, 256));
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            setContentPane(contentPane);
            contentPane.setLayout(new BorderLayout(0, 0));
           
            JTextArea textArea = new JTextArea();
            textArea.setEditable(false);
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            textArea.setText(message);
            contentPane.add(textArea, BorderLayout.CENTER);
           
            JButton button = new JButton("Close this");
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    dispose();
                }
            });
            contentPane.add(button, BorderLayout.SOUTH);
            pack();
            setSize(getWidth(), getHeight()  + textArea.getPreferredSize().height);
            setLocation(x - getWidth() / 2, y - getHeight() / 2);
            setVisible(true);
        }
 
    }
And this is all nice and dandy.
But i want my main application to be halted until the popup has been closed.

At first i tried it with the following code:
Code:
        Error_Popup win = new Error_Popup(title, message, x, y);
        while(win.isShowing()){}
But that doesnt work. I think this is because of the caching, if i add any random System.out.print message within the while-loop body it works.

So i tried:
Code:
        Error_Popup win = new Error_Popup(title, message, x, y);
        while(win.isShowing()){
            win.requestFocus();
        }
And this kinda works. But i dont like this solution, its quick and dirty.
So, is there any easy way i could have my main thread blocked until the jframe has been disposed?

Thanks in advance.
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,681
Use threads. Especially the usage of Thread.join().
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
Yeah i know that, but openGL with lwjgl is not supposed to be used with multiple threads. Its recommended to stay with the main thread.
But how do i make the main thread sleep?
 

tom_mai78101

The Helper Connoisseur / Ex-MineCraft Host
Staff member
Reaction score
1,681
Yeah i know that, but openGL with lwjgl is not supposed to be used with multiple threads. Its recommended to stay with the main thread.
But how do i make the main thread sleep?
What if I told you that your main thread isn't always the main execution thread you use when you start your application? ;)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top