Question

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();
    }
}
Was it helpful?

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

OTHER TIPS

Here's a class I put together for another project. It's closer to what you're looking for:

public class Splash extends JWindow implements Runnable
{

  private static final long serialVersionUID = -3945334716967506918L;

  public void run()
  {
  showSplash(10000);
  }

 private void showSplash(int duration)
 {
  JPanel content = (JPanel)getContentPane();

  //Set the windows bounds, centering the window
  int width = 558;
  int height = 430;

  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  int x = (screen.width-width)/2;
  int y = (screen.height-height)/2;
  setBounds(x,y,width,height);

  //build the splash screen
  //SPECIAL NOTE: WHEN ADDING A NEW IMAGE TO THE PROJECT, REFRESH ECLIPSE 
  //WITH F5 SO THAT THE FILE GETS RECOGNIZED. OTHERWISE YOU'LL GET NULLPOINTEREXCEPTION.
  JLabel label = new JLabel (new ImageIcon(getClass().getResource("data/images/World_Magnifier_Splash.jpg")));
  JLabel copyrt = new JLabel("Copyright 2012, Tactical Enterprises Ltd.", JLabel.CENTER);
  copyrt.setFont(new Font("Sans-Serif", Font.BOLD, 12));
  content.add(label, BorderLayout.CENTER);
  content.add(copyrt, BorderLayout.SOUTH);
  content.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));

  //display
  setVisible(true);

  try
  {
     Thread.sleep(duration);
  }
  catch(Exception e){}

  setVisible(false);
  }
}

You run it in a main method by creating a Thread, like so:

Thread t = new Thread(new Splash());
t.start();

Hope this helps.

Try to use JDialog instead of JWindow. The code below shows the dialog for 3 seconds.

public static void main(String[] args) {
    JDialog dialog = new JDialog();
    dialog.getContentPane().add(new JLabel("test"));
    dialog.pack();
    dialog.setVisible(true);
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    dialog.setVisible(false);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top