Java Swing Tutorial - Java Swing JWindow








JWindow is a top-level container window without a title bar, windows menu, etc.

import javax.swing.JFrame;
import javax.swing.JWindow;
/* ww  w  .  j a v a  2s.c  om*/
public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame("The Frame");
    f.setSize(300, 300);
    f.setLocation(100, 100);

    JWindow w = new JWindow();
    w.setSize(300, 300);
    w.setLocation(500, 100);

    f.setVisible(true);
    w.setVisible(true);
  }
}




Extending JWindow

If you need to extend JWindow, the class has two protected methods of importance:

protected void windowInit()
protected JRootPane createRootPane()
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*from w  ww .  j  a  v a2 s  .com*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;

public class JWindowNoTitleBar extends JFrame {
  JWindow window = new JWindow(this);

  public JWindowNoTitleBar() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.getContentPane().add(new JLabel("About"), BorderLayout.NORTH);
    window.getContentPane().add(new JLabel("Label", SwingConstants.CENTER),
        BorderLayout.CENTER);
    JButton b = new JButton("Close");
    window.getContentPane().add(b, BorderLayout.SOUTH);
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        window.setVisible(false);
      }
    });
    window.pack();
    window.setBounds(50, 50, 200, 200);

    b = new JButton("About...");
    b.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        window.setVisible(true);
      }
    });
    getContentPane().add(b);
    pack();
  }

  public static void main(String[] args) {
    new JWindowNoTitleBar().setVisible(true);
  }
}