Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a working solution. Basically we want to avoid showing the window if it was just closed by clicking on the button which also deactivates and hides the window. The mouseDown and windowDeactivated are both processed on the same input event, although the event times differ slightly. The action time can be much later since it is generated on the mouseUp. Using WindowAdapter is convenient to WindowListener and using the @Override annotation is good to avoid not having stuff work because of a typo.</p> <pre><code> public class PropertiesButton extends JButton { private JDialog theWindow; private long deactivateEventTime = System.currentTimeMillis(); private long mouseDownTime; public PropertiesButton(String text, final Frame launcher) { super(text); theWindow = new JDialog(); theWindow.getContentPane().add(new JLabel("Properties")); theWindow.pack(); // theWindow.setUndecorated(true); theWindow.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // theWindow.add(new JMenuCheckBoxItem("Something")); theWindow.addWindowListener(new WindowAdapter() { // just an example, need to implement other methods @Override public void windowDeactivated(WindowEvent e) { deactivateEventTime = EventQueue.getMostRecentEventTime(); theWindow.setVisible(false); } }); this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean alsoDeactivated = Math.abs(deactivateEventTime - mouseDownTime) &lt; 100; if (theWindow.isVisible()) { theWindow.setVisible(false); } else if (!alsoDeactivated) { // JButton btn = (JButton)e.getSource(); // theWindow.setLocation(btn.getLocationOnScreen().x,btn.getLocationOnScreen().x+50); theWindow.setVisible(true); } } }); theWindow.setVisible(false); } public void processMouseEvent(MouseEvent event) { if (event.getID() == MouseEvent.MOUSE_PRESSED) { mouseDownTime = event.getWhen(); } super.processMouseEvent(event); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload