Java Swing How to - Show loading screen with JWindow








Question

We would like to know how to show loading screen with JWindow.

Answer

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/*  w ww.ja va 2 s.  co m*/
import javax.swing.JPanel;
import javax.swing.JWindow;

public class Main {
  public static void main(String[] args) {
    LoadingScreenDemo m = new LoadingScreenDemo();
    m.setSize(640, 480);
    m.setLocationRelativeTo(null);
    m.setVisible(true);
  }
}

class LoadingScreenDemo extends JWindow {
  Panel panel = new Panel();
  public LoadingScreenDemo() {
    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
          dispose();
        }
      }
    });
    panel.setBackground(Color.YELLOW);
    add(panel);
  }
  class Panel extends JPanel {
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setFont(new Font("Verdana", Font.BOLD, 24));
      g.drawString("Loading...", 270, 210);
    }
  }
}