Example usage for javax.swing JFrame addWindowListener

List of usage examples for javax.swing JFrame addWindowListener

Introduction

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

Prototype

public synchronized void addWindowListener(WindowListener l) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*from  ww  w. j  a  va  2s  . c  om*/
        }
    });

    frame.getContentPane().add(new Main(), BorderLayout.CENTER);
    // Finally, set the size of the main window, and pop it up.
    frame.setSize(600, 400);
    frame.setVisible(true);
}

From source file:UIDefaultsTreeModel.java

public static void main(String[] args) {
    JFrame treeFrame1 = new JFrame();

    JScrollPane jScrollPane1 = new JScrollPane();
    JTree jTree1 = new JTree(new UIDefaultsTreeModel());

    treeFrame1.setSize(new Dimension(400, 300));
    treeFrame1.addWindowListener(new WindowAdapter() {
        public void windowClosed(WindowEvent e) {
            System.exit(0);//from   w  w  w .  j  a v a2s.c o m
        }
    });
    treeFrame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    treeFrame1.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTree1, null);

    treeFrame1.setVisible(true);
}

From source file:ClipImage.java

public static void main(String s[]) {
    final ClipImage demo = new ClipImage();
    WindowListener l = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*w  w w . j  a v a2 s .  com*/
        }

        public void windowDeiconified(WindowEvent e) {
            demo.start();
        }

        public void windowIconified(WindowEvent e) {
            demo.stop();
        }
    };
    JFrame f = new JFrame("Java 2D Demo - ClipImage");
    f.addWindowListener(l);
    f.getContentPane().add("Center", demo);
    f.setSize(new Dimension(400, 300));
    f.setVisible(true);
    demo.start();
}

From source file:DOMTreeWalkerTreeModel.java

/**
 * This main() method demonstrates the use of this class, the use of the
 * Xerces DOM parser, and the creation of a DOM Level 2 TreeWalker object.
 *///www  . j ava 2  s .  c  o m
public static void main(String[] args) throws IOException, SAXException {
    // Obtain an instance of a Xerces parser to build a DOM tree.
    // Note that we are not using the JAXP API here, so this
    // code uses Apache Xerces APIs that are not standards
    DOMParser parser = new org.apache.xerces.parsers.DOMParser();

    // Get a java.io.Reader for the input XML file and
    // wrap the input file in a SAX input source
    Reader in = new BufferedReader(new FileReader(args[0]));
    InputSource input = new org.xml.sax.InputSource(in);

    // Tell the Xerces parser to parse the input source
    parser.parse(input);

    // Ask the parser to give us our DOM Document. Once we've got the DOM
    // tree, we don't have to use the Apache Xerces APIs any more; from
    // here on, we use the standard DOM APIs
    Document document = parser.getDocument();

    // If we're using a DOM Level 2 implementation, then our Document
    // object ought to implement DocumentTraversal
    DocumentTraversal traversal = (DocumentTraversal) document;

    // For this demonstration, we create a NodeFilter that filters out
    // Text nodes containing only space; these just clutter up the tree
    NodeFilter filter = new NodeFilter() {
        public short acceptNode(Node n) {
            if (n.getNodeType() == Node.TEXT_NODE) {
                // Use trim() to strip off leading and trailing space.
                // If nothing is left, then reject the node
                if (((Text) n).getData().trim().length() == 0)
                    return NodeFilter.FILTER_REJECT;
            }
            return NodeFilter.FILTER_ACCEPT;
        }
    };

    // This set of flags says to "show" all node types except comments
    int whatToShow = NodeFilter.SHOW_ALL & ~NodeFilter.SHOW_COMMENT;

    // Create a TreeWalker using the filter and the flags
    TreeWalker walker = traversal.createTreeWalker(document, whatToShow, filter, false);

    // Instantiate a TreeModel and a JTree to display it
    JTree tree = new JTree(new DOMTreeWalkerTreeModel(walker));

    // Create a frame and a scrollpane to display the tree, and pop them up
    JFrame frame = new JFrame("DOMTreeWalkerTreeModel Demo");
    frame.getContentPane().add(new JScrollPane(tree));
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    frame.setSize(500, 250);
    frame.setVisible(true);
}

From source file:ClipImage.java

public static void main(String s[]) {
    final ClipImage demo = new ClipImage();
    WindowListener l = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);//from  w  ww  . j a v a2 s. co  m
        }

        public void windowDeiconified(WindowEvent e) {
            demo.start();
        }

        public void windowIconified(WindowEvent e) {
            demo.stop();
        }
    };
    JFrame f = new JFrame("Java 2D Demo - ClipImage");
    f.addWindowListener(l);
    f.getContentPane().add("Center", demo);
    f.setSize(new Dimension(400, 300));
    f.show();
    demo.start();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    device.setFullScreenWindow(frame);//from w w  w.  ja v a2s .co m
    device.setDisplayMode(new DisplayMode(800, 600, 32, 60));

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowIconified(WindowEvent we) {
            if (programmatic) {
                programmatic = false;
                frame.setState(JFrame.NORMAL);
            }
        }
    });

    JButton btn = new JButton();
    btn.setText("Btn");
    final JPanel panel = new JPanel();

    panel.add(btn);
    frame.add(panel);

    btn.addActionListener(e -> {
        programmatic = true;
        JOptionPane.showMessageDialog(panel, "Sample");
    });
    frame.setVisible(true);
}

From source file:EditorPaneExample9.java

public static void main(String[] args) {
    try {/*ww  w .  j  av a2s  . c  om*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new EditorPaneExample9();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    });
    f.setSize(500, 400);
    f.setVisible(true);
}

From source file:ShowComponent.java

public static void main(String[] args) {
    // Process the command line to get the components to display
    Vector components = getComponentsFromArgs(args);

    // Create a frame (a window) to display them in
    JFrame frame = new JFrame("ShowComponent");

    // Handle window close requests by exiting the VM
    frame.addWindowListener(new WindowAdapter() { // Anonymous inner class
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*from  www  .  ja v a 2s.c  o m*/
        }
    });

    // Set up a menu system that allows the user to select the
    // look-and-feel of the component from a list of installed PLAFs
    JMenuBar menubar = new JMenuBar(); // Create a menubar
    frame.setJMenuBar(menubar); // Tell the frame to display it
    JMenu plafmenu = createPlafMenu(frame); // Create a menu
    menubar.add(plafmenu); // Add the menu to the menubar

    // Create a JTabbedPane to display each of the components
    JTabbedPane pane = new JTabbedPane();

    // Now add each component as a tab of the tabbed pane
    // Use the unqualified component classname as the tab text
    for (int i = 0; i < components.size(); i++) {
        Component c = (Component) components.elementAt(i);
        String classname = c.getClass().getName();
        String tabname = classname.substring(classname.lastIndexOf('.') + 1);
        pane.addTab(tabname, c);
    }

    // Add the tabbed pane to the frame. Note the call to getContentPane()
    // This is required for JFrame, but not for most Swing components
    frame.getContentPane().add(pane);

    // Set the frame size and pop it up
    frame.pack(); // Make frame as big as its kids need
    frame.setVisible(true); // Make the frame visible on the screen

    // The main() method exits now but the Java VM keeps running because
    // all AWT programs automatically start an event-handling thread.
}

From source file:EditorPaneExample10A.java

public static void main(String[] args) {
    try {/*ww  w. j  a v  a 2 s. co  m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new EditorPaneExample10A();

    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    });
    f.setSize(500, 400);
    f.setVisible(true);
}

From source file:PanelDropTarget.java

public static void main(String[] args) {
    try {/*from   w  w w. j a v a 2  s .  c o  m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    final JFrame f = new JFrame("Component drop target example");

    JPanel pane = new JPanel();

    // Add a drop target to the JPanel
    PanelDropTarget target = new PanelDropTarget(pane);

    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    });

    f.getContentPane().add(new JScrollPane(pane), BorderLayout.CENTER);
    f.setSize(500, 400);
    f.setVisible(true);
}