Extending JWindow : JWindow « Swing « Java Tutorial






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

protected void windowInit()
protected JRootPane createRootPane()
Extending JWindow
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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








14.78.JWindow
14.78.1.Difference between JFrame and JWindow
14.78.2.Have borders on a JWindow/JFrame
14.78.3.Extending JWindowExtending JWindow
14.78.4.Center window
14.78.5.Center Within Parent