What I asked originally didn't clearly state my question/problem, so I will explain it better. I have a JButton
that sets a JDialog
to visible. The JDialog has a WindowListener
that sets it to NOT visible on the windowDeactivated()
event, which is triggered anytime the user clicks outside of the dialog. The button ActionListener
checks if the dialog isVisible, hides it if true, shows it if false.
windowDeactivated()
will always trigger whether clicking on the button or not, as long as the user clicks outside the dialog. The problem I'm having is when the user clicks the button to close the dialog. The dialog is closed by the WindowListener
and then the ActionListener
tries to display it.
If windowDeactivated()
doesn't setVisible(false)
, then the dialog is still open, but behind the parent window. What I'm asking for is how to get access to the location of the click inside windowDeactivated()
. If I know that the user clicked on the button and windowDeactivated() can skip hiding the dialog, so that the button's ActionListener
will see that it's still visible and hide it.
public PropertiesButton extends JButton { private JDialog theWindow; public PropertiesButton() { theWindow = new JDialog(); theWindow.setUndecorated(true); theWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); theWindow.add(new JMenuCheckBoxItem("Something")); theWindow.addWindowListener(new WindowListener() { // just an example, need to implement other methods public void windowDeactivated(WindowEvent e) { theWindow.setVisible(false); } }); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (theWindow.isVisible()) { theWindow.setVisible(false); } else { JButton btn = (JButton)e.getSource(); theWindow.setLocation(btn.getLocationOnScreen.x,btn.getLocationOnScreen.x-50); theWindow.setVisible(true); } } }); theWindow.setVisible(false); } }
WindowAdapter
instead of a window listener. Then you only have to implement the methods you want. – Guanidine