Search code examples
javamultithreadingswingconcurrencyjwindow

Show a JWindow for a while


I want to show a message in a JWindow for a while, so I tryed to use while() and sleep() functions, but it doesn't work. This is the function that should call JWindow ( MessageWindow ). Is there any other way to show this window for 2 seconds?

private void showJWindow() {
    boolean flag = true;
    final MessageWindow window = new MessageWindow( playerName, playerInAction );
    window.setVisible( true );

    try {
        synchronized( window ) {
            while( flag ) {
                window.wait( 3000 );
                flag = false;
                window.setVisible( false );
            }
        }

    } catch( InterruptedException ie ) {
        Thread.currentThread().interrupt();
    }
}

Solution

  • Here is a short example using a Swing timer.

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class TestGUI {
        public TestGUI() {
            final JFrame frame = new JFrame("Test");
            JButton press = new JButton("Press Me");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.add(press);
            press.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    final JDialog dialog = new JDialog();
                    dialog.add(new JLabel("Here for 2 seconds"));
                    dialog.pack();
                    dialog.setLocationRelativeTo(null);
                    dialog.setVisible(true);
                    Timer timer = new Timer(2000, new ActionListener() {
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            dialog.setVisible(false);
                            dialog.dispose();
                        }
                    });
                    timer.setRepeats(false);
                    timer.start();
                }
            });
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TestGUI();
                }
            });
        }
    }