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:GridBagWithWeight.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of fill constraints");
    JPanel p = new JPanel(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;/*from  ww w. j ava 2s.c  o m*/
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH; // Use both horizontal & vertical
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.fill = GridBagConstraints.HORIZONTAL; // Horizontal only
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE; // Remember to reset to none
    p.add(new JButton("and"), c);
    c.gridx = 2;
    c.fill = GridBagConstraints.VERTICAL; // Vertical only
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:lcmc.LCMC.java

/** The main function for starting the application. */
public static void main(final String[] args) {
    Tools.init();//from   w  ww.  j av  a 2  s .  c om
    final JFrame mainFrame = new JFrame(Tools.getString("DrbdMC.Title") + " " + Tools.getRelease());
    final List<Image> il = new ArrayList<Image>();
    for (final String iconS : new String[] { "LCMC.AppIcon32", "LCMC.AppIcon48", "LCMC.AppIcon64",
            "LCMC.AppIcon128", "LCMC.AppIcon256" }) {
        il.add(Tools.createImageIcon(Tools.getDefault(iconS)).getImage());
    }
    mainFrame.setIconImages(il);
    final String autoArgs = initApp(args);
    mainFrame.setGlassPane(getMainGlassPane());
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.addWindowListener(new ExitListener());
    mainFrame.setJMenuBar(getMenuBar());
    mainFrame.setContentPane(getMainPanel());
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI((Container) mainFrame);
        }
    });
    if (autoArgs != null) {
        Tools.parseAutoArgs(autoArgs);
    }
    //final Thread t = new Thread(new Runnable() {
    //    public void run() {
    //        drbd.utilities.RoboTest.startMover(600000, true);
    //    }
    //});
    //t.start();
}

From source file:GridBagWithContaints.java

public static void main(String[] args) {
    JFrame f = new JFrame("Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
    JPanel p = new JPanel();

    p.setLayout(new GridBagLayout());
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2); // insets for all components
    c.gridx = 0; // column 0
    c.gridy = 0; // row 0
    c.ipadx = 5; // increases components width by 10 pixels
    c.ipady = 5; // increases components height by 10 pixels
    p.add(new JButton("Java"), c); // constraints passed in
    c.gridx = 1; // column 1
    // c.gridy = 0; // comment out this for reusing the obj
    c.ipadx = 0; // resets the pad to 0
    c.ipady = 0;//from  w  w w.  j  av  a  2s  . c  om
    p.add(new JButton("Source"), c);
    c.gridx = 0; // column 0
    c.gridy = 1; // row 1
    p.add(new JButton("and"), c);
    c.gridx = 1; // column 1
    p.add(new JButton("Support."), c);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    f.addWindowListener(wndCloser);

    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
}

From source file:DragDropTreeExample.java

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

    final JFrame f = new JFrame("FileTree Drop Target Example");

    try {
        final FileTree tree = new FileTree("D:\\");

        // Add a drop target to the FileTree
        FileTreeDropTarget target = new FileTreeDropTarget(tree);

        tree.setEditable(true);

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

        JPanel panel = new JPanel();
        final JCheckBox editable = new JCheckBox("Editable");
        editable.setSelected(true);
        panel.add(editable);
        editable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEditable(editable.isSelected());
            }
        });

        final JCheckBox enabled = new JCheckBox("Enabled");
        enabled.setSelected(true);
        panel.add(enabled);
        enabled.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEnabled(enabled.isSelected());
            }
        });

        f.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
        f.getContentPane().add(panel, BorderLayout.SOUTH);
        f.setSize(500, 400);
        f.setVisible(true);
    } catch (Exception e) {
        System.out.println("Failed to build GUI: " + e);
    }
}

From source file:DragDropTreeExample.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("FileTree Drop and Drop Example");
    try {
        final FileTree tree = new FileTree("D:\\");

        // Add a drop target to the FileTree
        FileTreeDropTarget target = new FileTreeDropTarget(tree);

        // Add a drag source to the FileTree
        FileTreeDragSource source = new FileTreeDragSource(tree);

        tree.setEditable(true);

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

        JPanel panel = new JPanel();
        final JCheckBox editable = new JCheckBox("Editable");
        editable.setSelected(true);
        panel.add(editable);
        editable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEditable(editable.isSelected());
            }
        });

        final JCheckBox enabled = new JCheckBox("Enabled");
        enabled.setSelected(true);
        panel.add(enabled);
        enabled.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                tree.setEnabled(enabled.isSelected());
            }
        });

        f.getContentPane().add(new JScrollPane(tree), BorderLayout.CENTER);
        f.getContentPane().add(panel, BorderLayout.SOUTH);
        f.setSize(500, 400);
        f.setVisible(true);
    } catch (Exception e) {
        System.out.println("Failed to build GUI: " + e);
    }
}

From source file:com.adito.upgrade.Upgrade.java

/**
 * @param args/*from   w ww .ja  v a  2s .  co m*/
 * @throws Exception on any error
 */
public static void main(String[] args) throws Exception {

    boolean gui = System.getProperty("os.name").toLowerCase().startsWith("windows")
            || System.getenv("DISPLAY") != null;

    if (args.length == 2 || !gui) {
        Upgrader upgrader = new CommandLineUpgrader(args);
        upgrader.upgrade();
    } else {
        JFrame f = new JFrame("0.1.16 to 0.2.5+ Upgrader");
        final Upgrader upgrader = new GUIUpgrader();
        f.setIconImage(new ImageIcon(Upgrade.class.getResource("upgrader-32x32.png")).getImage());
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add((JPanel) upgrader, BorderLayout.CENTER);
        f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        JPanel bp = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        final JButton start = new JButton("Start");
        ;
        final JButton close = new JButton("Close");
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    start.setEnabled(false);
                    close.setEnabled(false);
                    upgrader.upgrade();
                } catch (Exception ex) {
                    upgrader.error("Failed to upgrade.", ex);
                } finally {
                    close.setEnabled(true);
                }
            }
        });
        close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (close.isEnabled())
                    System.exit(0);
            }
        });
        bp.add(start);
        bp.add(close);
        f.getContentPane().add(bp, BorderLayout.SOUTH);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                if (close.isEnabled())
                    System.exit(0);
            }
        });
        f.setSize(new Dimension(480, 460));
        UIUtil.positionComponent(SwingConstants.CENTER, f);
        f.setVisible(true);
    }
}

From source file:org.jets3t.apps.uploader.Uploader.java

/**
 * Run the Uploader as a stand-alone application.
 *
 * @param args/*from  w w w. j  a  v  a2  s  .com*/
 * @throws Exception
 */
public static void main(String args[]) throws Exception {
    JFrame ownerFrame = new JFrame("JetS3t Uploader");
    ownerFrame.addWindowListener(new WindowListener() {
        public void windowOpened(WindowEvent e) {
        }

        public void windowClosing(WindowEvent e) {
            e.getWindow().dispose();
        }

        public void windowClosed(WindowEvent e) {
        }

        public void windowIconified(WindowEvent e) {
        }

        public void windowDeiconified(WindowEvent e) {
        }

        public void windowActivated(WindowEvent e) {
        }

        public void windowDeactivated(WindowEvent e) {
        }
    });

    // Read arguments as properties of the form: <propertyName>'='<propertyValue>
    Properties argumentProperties = new Properties();
    if (args.length > 0) {
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            int delimIndex = arg.indexOf("=");
            if (delimIndex >= 0) {
                String name = arg.substring(0, delimIndex);
                String value = arg.substring(delimIndex + 1);
                argumentProperties.put(name, value);
            } else {
                System.out.println("Ignoring property argument with incorrect format: " + arg);
            }
        }
    }

    new Uploader(ownerFrame, argumentProperties);
}

From source file:Main.java

public static void main(String[] a) {
    final JFrame jf = new JFrame("JIFrameDemo Main Window");

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    screenSize.width -= 42;/*from w w  w .j  a va  2  s  .  c  om*/
    screenSize.height -= 42;
    jf.setSize(screenSize);
    jf.setLocation(20, 20);

    JMenuBar mb = new JMenuBar();
    jf.setJMenuBar(mb);
    JMenu fm = new JMenu("File");
    mb.add(fm);
    JMenuItem mi;
    fm.add(mi = new JMenuItem("Exit"));
    mi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    JDesktopPane dtp = new JDesktopPane();
    //dtp.setBackground(Color.GREEN);
    jf.setContentPane(dtp);

    JInternalFrame mboxFrame = new JInternalFrame("Mail Reader", true, true, true, true);
    JLabel reader = new JLabel("Mail Reader Would Be Here");
    mboxFrame.setContentPane(reader);
    mboxFrame.setSize(400, 300);
    mboxFrame.setLocation(50, 50);
    mboxFrame.setVisible(true);
    dtp.add(mboxFrame);

    JInternalFrame compFrame = new JInternalFrame("Compose Mail", true, true, true, true);
    JLabel composer = new JLabel("Mail Compose Would Be Here");
    compFrame.setContentPane(composer);
    compFrame.setSize(300, 200);
    compFrame.setLocation(200, 200);
    compFrame.setVisible(true);
    dtp.add(compFrame);

    JInternalFrame listFrame = new JInternalFrame("Users", true, true, true, true);
    JLabel list = new JLabel("List of Users Would Be Here");
    listFrame.setContentPane(list);
    listFrame.setLocation(400, 400);
    listFrame.setSize(500, 200);
    listFrame.setVisible(true);
    dtp.add(listFrame);

    jf.setVisible(true);
    jf.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            jf.setVisible(false);
            jf.dispose();
            System.exit(0);
        }
    });
}

From source file:TextAcceleratorExample.java

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

    JLabel l;
    JTextField t;
    JButton b;
    JFrame f = new JFrame("Text Accelerator Example");
    Container cp = f.getContentPane();
    cp.setLayout(new GridBagLayout());
    cp.setBackground(UIManager.getColor("control"));
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = GridBagConstraints.RELATIVE;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.insets = new Insets(2, 2, 2, 2);
    c.anchor = GridBagConstraints.EAST;

    cp.add(l = new JLabel("Name:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('n');
    cp.add(l = new JLabel("House/Street:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('h');
    cp.add(l = new JLabel("City:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('c');
    cp.add(l = new JLabel("State/County:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('s');
    cp.add(l = new JLabel("Zip/Post code:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('z');
    cp.add(l = new JLabel("Telephone:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('t');
    cp.add(b = new JButton("Clear"), c);
    b.setMnemonic('l');

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.CENTER;

    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('n');
    c.gridx = 1;
    c.gridy = GridBagConstraints.RELATIVE;
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('h');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('c');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('s');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('z');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('t');
    c.weightx = 0.0;
    c.fill = GridBagConstraints.NONE;
    cp.add(b = new JButton("OK"), c);
    b.setMnemonic('o');

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

From source file:edu.ku.brc.specify.tools.StrLocalizerApp.java

/**
  * @param args/*from   w ww . j  a  va  2s. c om*/
  */
public static void main(String[] args) {
    setAppName("Specify"); //$NON-NLS-1$
    System.setProperty(AppPreferences.factoryName, "edu.ku.brc.specify.config.AppPrefsDBIOIImpl"); // Needed by AppReferences //$NON-NLS-1$

    for (String s : args) {
        String[] pairs = s.split("="); //$NON-NLS-1$
        if (pairs.length == 2) {
            if (pairs[0].startsWith("-D")) //$NON-NLS-1$
            {
                //System.err.println("["+pairs[0].substring(2, pairs[0].length())+"]["+pairs[1]+"]");
                System.setProperty(pairs[0].substring(2, pairs[0].length()), pairs[1]);
            }
        } else {
            String symbol = pairs[0].substring(2, pairs[0].length());
            //System.err.println("["+symbol+"]");
            System.setProperty(symbol, symbol);
        }
    }

    // Now check the System Properties
    String appDir = System.getProperty("appdir");
    if (StringUtils.isNotEmpty(appDir)) {
        UIRegistry.setDefaultWorkingPath(appDir);
    }

    String appdatadir = System.getProperty("appdatadir");
    if (StringUtils.isNotEmpty(appdatadir)) {
        UIRegistry.setBaseAppDataDir(appdatadir);
    }

    // Then set this
    IconManager.setApplicationClass(Specify.class);
    IconManager.loadIcons(XMLHelper.getConfigDir("icons.xml")); //$NON-NLS-1$
    //ImageIcon icon = IconManager.getIcon("AppIcon", IconManager.IconSize.Std16);

    try {
        ResourceBundle.getBundle("resources", Locale.getDefault()); //$NON-NLS-1$

    } catch (MissingResourceException ex) {
        Locale.setDefault(Locale.ENGLISH);
        UIRegistry.setResourceLocale(Locale.ENGLISH);
    }

    try {
        if (!System.getProperty("os.name").equals("Mac OS X")) {
            UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
            PlasticLookAndFeel.setPlasticTheme(new DesertBlue());
        }
    } catch (Exception e) {
        //whatever
    }
    AppPreferences localPrefs = AppPreferences.getLocalPrefs();
    localPrefs.setDirPath(UIRegistry.getAppDataDir());

    boolean doIt = false;
    if (doIt)

    {
        Charset utf8charset = Charset.forName("UTF-8");
        Charset iso88591charset = Charset.forName("ISO-8859-1");

        ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[] { (byte) 0xC3, (byte) 0xA2 });

        // decode UTF-8
        CharBuffer data = utf8charset.decode(inputBuffer);

        // encode ISO-8559-1
        ByteBuffer outputBuffer = iso88591charset.encode(data);
        byte[] outputData = outputBuffer.array();
        System.out.println(new String(outputData));
        return;
    }

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIHelper.OSTYPE osType = UIHelper.getOSType();
                if (osType == UIHelper.OSTYPE.Windows) {
                    UIManager.setLookAndFeel(new PlasticLookAndFeel());
                    PlasticLookAndFeel.setPlasticTheme(new ExperienceBlue());

                } else if (osType == UIHelper.OSTYPE.Linux) {
                    UIManager.setLookAndFeel(new PlasticLookAndFeel());
                }
            } catch (Exception e) {
                log.error("Can't change L&F: ", e); //$NON-NLS-1$
            }

            JFrame frame = new JFrame(getResourceString("StrLocalizerApp.AppTitle"));
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            final StrLocalizerApp sl = new StrLocalizerApp();
            sl.addMenuBar(frame);
            frame.setContentPane(sl);
            frame.setSize(768, 1024);

            //Dimension size = frame.getPreferredSize();
            //size.height = 500;
            //frame.setSize(size);
            frame.addWindowListener(sl);
            IconManager.setApplicationClass(Specify.class);
            frame.setIconImage(IconManager.getImage(IconManager.makeIconName("SpecifyWhite32")).getImage());
            UIHelper.centerAndShow(frame);

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    sl.startUp();
                }
            });
        }
    });
}