There is a bean property "visible", which is represented by getter isVisible() and setter setVisible() in class Window.
How to listen for this value?
I would like to implement popular "view" menu with check box with binding library. Unfortunately I can't see how to bind "visible" property of a window. Even I can't write translator, because I see no any predefined way to listen for this property:
package tests;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Try_Swing2 {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame2 = new JFrame();
frame2.addWindowStateListener(new WindowStateListener() {
@Override
public void windowStateChanged(WindowEvent e) {
System.out.println("windowState.newState = " + e.getNewState());
}
});
frame2.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("windowOpened");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("windowIconified");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("windowDeiconified");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("windowDeactivated");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("windowClosing");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("windowClosed");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("windowActivated");
}
});
frame2.addPropertyChangeListener("visible", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("visible = " + evt.getNewValue());
}
});
frame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame2.setTitle("This window is controlled by another window");
frame2.setSize(800, 600);
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
AbstractAction toggleAction = new AbstractAction("Toggle another window visibility") {
@Override
public void actionPerformed(ActionEvent e) {
frame2.setVisible( !frame2.isVisible() );
}
};
JButton toggleButton = new JButton(toggleAction);
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setTitle("This windows controls");
frame1.setLayout(new FlowLayout());
frame1.add(toggleButton);
frame1.pack();
frame1.setLocation(0, 0);
frame1.setVisible(true);
}
});
}
}
The visible property is not really bound to the WindowsListener, but to the ComponentListener because it belong to the Component class, not the Window class.
In order to listen to changes in the visible property, the componentShown(ComponentEvent e) method of the ComponentListener needs to be implemented. It is always easier to inherit from the adapters, so in this case:
frame2.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
System.out.println("Component is Visible");
}
});
The adapters have empty implementations of the listeners, e.g. ComponentAdapter class which has empty implementations of the methods in ComponentListener, and WindowAdapter from WindowListener.
For more details, see Component Adapter, Component Listener, and How to Write a Component Listener and
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With