Java method that runs when window is opened

别等时光非礼了梦想. 提交于 2019-12-11 18:10:02

问题


I need to set up a listener that can call a method every time a show() method is invoked to display a window. How can I do this?


回答1:


You'd probably be interested in WindowListener.

From the tutorial, "How to Write Window Listeners":

The following window activities or states can precede a window event:

  • Opening a window — Showing a window for the first time.
  • Closing a window — Removing the window from the screen.
  • Iconifying a window — Reducing the window to an icon on the desktop.
  • Deiconifying a window — Restoring the window to its original size.
  • Focused window — The window which contains the "focus owner".
  • Activated window (frame or dialog) — This window is either the focused window, or owns the focused window.
  • Deactivated window — This window has lost the focus. For more information about focus, see the AWT Focus Subsystem specification.

If you don't want to have to implement all of them, you could use a WindowAdapter, as follows:

myFrame.addWindowListener(new WindowAdapter() {
    @Override
    public void windowOpened(WindowEvent we) {
        System.out.println("this window was opened for the first time");
    }

    @Override
    public void windowActivated(WindowEvent we) {
        System.out.println("this window or a subframe was focused");
    }
});



回答2:


Add a ComponentListener and check for the componentShown event. Note that a WindowListener will only fire the first time the window is opened while ComponentListener will fire every time.

See How to Write a Component Listener for more details.

This source demonstrates the two listeners.

import java.awt.*;
import java.awt.event.*;
import java.util.logging.*;
import javax.swing.*;
import javax.swing.event.*;

class DetectVisibility {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout());
                final Logger log = Logger.getAnonymousLogger();

                ComponentListener cl = new ComponentListener() {

                    @Override
                    public void componentResized(ComponentEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void componentMoved(ComponentEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void componentShown(ComponentEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void componentHidden(ComponentEvent e) {
                        log.log(Level.INFO, e.toString());
                    }
                };

                WindowListener we = new WindowListener() {

                    @Override
                    public void windowOpened(WindowEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void windowClosing(WindowEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void windowClosed(WindowEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void windowIconified(WindowEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void windowDeiconified(WindowEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void windowActivated(WindowEvent e) {
                        log.log(Level.INFO, e.toString());
                    }

                    @Override
                    public void windowDeactivated(WindowEvent e) {
                        log.log(Level.INFO, e.toString());
                    }
                };

                final JWindow w = new JWindow();
                w.setSize(100,100);
                w.setLocation(10,10);
                w.addComponentListener(cl);
                w.addWindowListener(we);

                final JCheckBox show = new JCheckBox("Show Window");
                ChangeListener chl = new ChangeListener() {

                    @Override
                    public void stateChanged(ChangeEvent e) {
                        w.setVisible(show.isSelected());
                    }
                };
                show.addChangeListener(chl);
                gui.add(show);

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}



回答3:


There is an interface called WindowListener and here is a event that gets fired when window is opened.You can find all the events in the first link.

EDIT :

The WindowListener works only once. As Per @Andrew Thompson's post we should be using ComponentListener. Certainly the best answer.



来源:https://stackoverflow.com/questions/15469904/java-method-that-runs-when-window-is-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!