Example usage for javax.swing JFrame addWindowStateListener

List of usage examples for javax.swing JFrame addWindowStateListener

Introduction

In this page you can find the example usage for javax.swing JFrame addWindowStateListener.

Prototype

public synchronized void addWindowStateListener(WindowStateListener l) 

Source Link

Document

Adds the specified window state listener to receive window events from this window.

Usage

From source file:Main.java

public Main() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel() {
        @Override//from  w w w .ja v  a2  s .  c  om
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    };
    frame.add(panel);
    frame.addWindowStateListener(new WindowAdapter() {
        @Override
        public void windowStateChanged(WindowEvent we) {
            super.windowStateChanged(we);
            if (we.getNewState() == Frame.ICONIFIED) {
                System.out.println("Here");
            }
        }
    });
    frame.pack();
    frame.setVisible(true);
}

From source file:com.tag.FramePreferences.java

public void install() {
    windowStateListener = new WindowStateListener() {

        public void windowStateChanged(WindowEvent e) {
            Object source = e.getSource();
            if (source instanceof JFrame) {
                JFrame frame = (JFrame) source;
                int extendedState = frame.getExtendedState();
                if (extendedState == JFrame.ICONIFIED)
                    return;

                Preferences prefs = getPreferences();
                prefs.putInt(KEY_EXTENDED_STATE, extendedState);
            }//from  w w w .jav a  2  s.c o  m
        }

    };
    frame.addWindowStateListener(windowStateListener);

    componentListener = new ComponentAdapter() {

        @Override
        public void componentResized(ComponentEvent e) {
            Preferences prefs = getPreferences();
            Dimension size = frame.getSize();
            prefs.putInt(KEY_WIDTH, size.width);
            prefs.putInt(KEY_HEIGHT, size.height);
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            Preferences prefs = getPreferences();
            Point location = frame.getLocation();
            prefs.putInt(KEY_X, location.x);
            prefs.putInt(KEY_Y, location.y);
        }

    };
    frame.addComponentListener(componentListener);
}