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:au.com.jwatmuff.eventmanager.util.GUIUtils.java

/**
 * Makes a frame visible and blocks the caller until the frame is closed.
 * /*from www.  j av a  2s .  c o  m*/
 * @param frame
 */
public static void runModalJFrame(final JFrame frame) {
    // there may be a much better way of implementing this, i don't know..
    class RunningFlag {
        boolean value = true;
    }

    final RunningFlag flag = new RunningFlag();
    final Thread t = Thread.currentThread();

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent arg0) {
                        synchronized (t) {
                            flag.value = false;
                            t.notifyAll();
                        }
                    }
                });

                frame.setVisible(true);

            }
        });

        synchronized (t) {
            while (flag.value == true)
                try {
                    t.wait();
                } catch (InterruptedException e) {
                }
        }
    } catch (InterruptedException e) {
        log.error(e);
    } catch (InvocationTargetException e2) {
        log.error(e2);
    }
}

From source file:SwingTest.java

static protected void registerWindowListener(JFrame frame) {
    // disable automatic close support for Swing frame.
    frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

    // adds the window listener
    frame.addWindowListener(new WindowAdapter() {
        // handles the system exit window message
        public void windowClosing(WindowEvent e) {
            System.exit(1);//from  www .  j a  va  2  s .  co m
        }
    });
}

From source file:com.db4o.sync4o.ui.Db4oSyncSourceConfigPanel.java

private static void showTestUI() {

    // Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    // Create and set up the window.
    JFrame frame = new JFrame("Db4oSyncSourceConfigPanel Test Harness");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    final Db4oSyncSource source = new Db4oSyncSource();
    Db4oSyncSourceConfigPanel p = new Db4oSyncSourceConfigPanel();
    p.setManagementObject(new SyncSourceManagementObject(source, null, null, null, null));
    p.updateForm();/*w  w w .j a va  2 s . c o m*/
    p.setOpaque(true); // content panes must be opaque
    frame.setContentPane(p);

    frame.addWindowListener(new WindowAdapter() {

        public void windowClosing(WindowEvent ev) {

            XMLEncoder encoder = null;
            try {
                FileOutputStream s = new FileOutputStream("test.xml");
                encoder = new XMLEncoder(s);
                encoder.setExceptionListener(new ExceptionListener() {
                    public void exceptionThrown(Exception exception) {
                        exception.printStackTrace();
                    }
                });
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                System.exit(-1);
            }
            encoder.writeObject((Object) source);
            encoder.flush();
            encoder.close();

        }

    });

    // Display the window.
    frame.pack();
    frame.setVisible(true);

}

From source file:org.geotools.renderer.chart.GeometryRendererTest.java

void showChart(XYPlot plot) throws Exception {
    JFreeChart chart = new JFreeChart(plot);
    chart.setAntiAlias(true);// w ww .jav a 2  s. c  o m
    ChartPanel panel = new ChartPanel(chart, true);

    final String headless = System.getProperty("java.awt.headless", "false");
    if (!headless.equalsIgnoreCase("true") && TestData.isInteractiveTest()) {
        try {
            JFrame frame = new JFrame(getName());
            frame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    e.getWindow().dispose();
                }
            });
            frame.setContentPane(panel);
            frame.setSize(new Dimension(500, 500));
            frame.setVisible(true);

            Thread.sleep(5000);
            frame.dispose();
        } catch (HeadlessException exception) {
            // The test is running on a machine without X11 display. Ignore.
            return;
        }
    }
}

From source file:network.Draw.java

@Override
public void run() {
    JFrame frame = new JFrame();
    frame.addWindowListener(new java.awt.event.WindowAdapter() {
        @Override//from  w  w w. ja v a  2 s  .c o  m
        public void windowClosing(java.awt.event.WindowEvent windowEvent) {
            isRun = false;
        }
    });

    while (isRun) {
        JFreeChart xylineChart;
        if (method == 0) {
            System.out.println("hi");
            xylineChart = ChartFactory.createXYLineChart("Tempreture", "Time", "Tempreture", createDataset(0),
                    PlotOrientation.VERTICAL, true, true, false);
        } else {
            System.out.println("hi");
            xylineChart = ChartFactory.createXYLineChart("Tempreture", "Time", "Tempreture", createDataset(1),
                    PlotOrientation.VERTICAL, true, true, false);
        }
        final XYPlot plot = xylineChart.getXYPlot();
        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesPaint(0, Color.RED);
        renderer.setSeriesStroke(0, new BasicStroke(1.0f));
        plot.setRenderer(renderer);

        frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        frame.setSize(700, 500);
        frame.getContentPane().removeAll();

        ChartPanel chartPanel = new ChartPanel(xylineChart);
        frame.add(chartPanel);
        frame.setVisible(true);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }
}

From source file:org.jfree.chart.demo.MeterChartDemo.java

/**
 * Displays a meter chart./*from   ww  w . ja  v  a2  s .  c o m*/
 *
 * @param value  the value.
 * @param shape  the dial shape.
 */
void displayMeterChart(final double value, final DialShape shape) {

    final DefaultValueDataset data = new DefaultValueDataset(75.0);
    final MeterPlot plot = new MeterPlot(data);
    plot.setUnits("Degrees");
    plot.setRange(new Range(20.0, 140.0));
    //        plot.setNormalRange(new Range(70.0, 100.0));
    //      plot.setWarningRange(new Range(100.0, 120.0));
    //    plot.setCriticalRange(new Range(120.0, 140.0));

    plot.setDialShape(shape);
    plot.setNeedlePaint(Color.white);
    plot.setTickLabelFont(new Font("SansSerif", Font.BOLD, 9));

    //  plot.setInsets(new Insets(5, 5, 5, 5));
    final JFreeChart chart = new JFreeChart("Meter Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

    //        final MeterLegend legend = new MeterLegend("Sample Meter");
    //      chart.setLegend(legend);

    chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));

    final JFrame chartFrame = new ChartFrame("Meter Chart", chart);
    chartFrame.addWindowListener(new WindowAdapter() {
        /**
         * Invoked when a window is in the process of being closed.
         * The close operation can be overridden at this point.
         */
        public void windowClosing(final WindowEvent e) {
            System.exit(0);
        }
    });
    chartFrame.pack();
    RefineryUtilities.positionFrameRandomly(chartFrame);
    chartFrame.setSize(250, 250);
    chartFrame.setVisible(true);

}

From source file:net.fenyo.gnetwatch.GUI.AwtGUI.java

/**
 * Adds a component to a frame and displays the frame.
 * This method can be run from any thread, and particularly the SWT thread.
 * @param frame frame./*from   w  w  w .  j  a  v  a 2 s  .c  om*/
 * @param component component to add.
 */
// GUI thread
public JFrame createFrame(final String name, final BasicComponent component)
        throws InterruptedException, InvocationTargetException {
    final JFrame frame = new JFrame(name);
    frame.addWindowListener(component);
    javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
        public void run() {
            _createFrame(frame, component);
        }
    });
    synchronized (frame_list) {
        frame_list.add(frame);
    }
    return frame;
}

From source file:net.fenyo.gnetwatch.GUI.AwtGUI.java

public void createDialog() {
    try {//from   w w w.ja  v  a 2 s. co m
        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                final JTextField button = new JTextField();
                button.setText("close this window to stop the foreground action");
                final JFrame frame = new JFrame("working...");
                frame.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        // XXX
                    }
                });

                frame.getContentPane().add(button, BorderLayout.CENTER);
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                frame.toFront();
            }
        });
    } catch (final InvocationTargetException ex) {
        log.error("exception", ex);
    } catch (final InterruptedException ex) {
        log.error("exception", ex);
    }
}

From source file:net.brtly.monkeyboard.gui.MasterControlPanel.java

private void initWindowListener(final JFrame frame) {
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {
        public void windowOpened(WindowEvent e) {

        }// w  w w.j av  a 2  s . c  o  m

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

        public void windowClosed(WindowEvent e) {
            for (Runnable onClose : _runOnClose) {
                onClose.run();
            }
        }
    });
}

From source file:org.jcodec.player.app.PlayerMain.java

public PlayerMain(URL url) throws IOException {
    JFrame frame = new JFrame("Player");

    vo = new SwingVO();
    frame.getContentPane().add(vo, BorderLayout.CENTER);

    // Finish setting up the frame, and show it.
    frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/* w ww.j ava 2  s.c  o  m*/
        }
    });
    vo.setVisible(true);

    File cacheWhere = new File(System.getProperty("user.home"), "Library/JCodec");
    FileUtils.forceMkdir(cacheWhere);

    HttpMedia http = new HttpMedia(url, cacheWhere);

    final HttpPacketSource videoTrack = http.getVideoTrack();
    video = new JCodecVideoSource(videoTrack);

    List<HttpPacketSource> audioTracks = http.getAudioTracks();
    AudioSource[] audio = new AudioSource[audioTracks.size()];
    for (int i = 0; i < audioTracks.size(); i++) {
        audio[i] = new JCodecAudioSource(audioTracks.get(i));
    }
    mixer = new AudioMixer(2, audio);

    player = new Player(video, mixer, vo, new JSoundAudioOut());

    frame.addKeyListener(this);

    frame.pack();
    frame.setVisible(true);
    frame.setSize(new Dimension(768, 596));

    player.play();
}