Managing window types - Java Swing

Java examples for Swing:JFrame

Introduction

The JFrame class supports a setType method.

setType method configures the general appearance of a window to one of the three types.

setType method can simplify the setting of a window's appearance.

Demo Code

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Main extends JFrame {

    public Main() {
        this.setTitle("Example");
        this.setBounds(100, 100, 200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setType(Type.UTILITY);
        this.setLayout(new FlowLayout());
        /*from www . j  a va2  s  . c  om*/
        JButton exitButton = new JButton("Exit");
        this.add(exitButton);
        exitButton.addActionListener(event->System.exit(0));
    }
    public static void main(String[] args) {               
        SwingUtilities.invokeLater(() ->{
                Main window = new Main();
                window.setVisible(true);
        });

    }
}

To set the window type, use the setType method with one of the three window types:

  • Type.NORMAL: represents a normal window and is the default value for windows
  • Type.POPUP: a temporary window intended to be used for small areas, such as tool tips
  • Type.UTILITY: a small window for objects, such as a palette

Related Tutorials