0

------------Edit---------------

Question:I have written a JTextField which has a Popup.This popup is an undecorated JFrame.Since the codes has already written by other people,Now I want to implements follow:

1.When the popup lost focus, close the popup.

2.When use clicked on JTextField, if popup is open,close the popup ,else open the popup.

How to implement this?

Following is my attempt,but there is a conflict on it.


Firstly, I have a JTextField with MouseListener using to trigger a JFrame Popup.

      public void mousePressed(MouseEvent arg0) {
            if (TextField.this.isEnabled() && !popup.isVisible() && TextField.this.isEditable())
                open();
            else if (popup.isVisible()) {
                popup.setVisible(false);
            }
        }

I add the windowfocuslistener to the JFrame(popup) to ensure it can be close when focus lost.like follow:

       popup.addWindowFocusListener(new WindowFocusListener() {

             @Override
             public void windowLostFocus(WindowEvent e) {
                    popup.setVisible(false);
             }
       }

So the problem came,when I click the JTextField firstly, it open the Date popup,when I clicked the JTextField again, it will first call windowlostfocus to setvisible(false).then mouselisten, will open the Date Popup. This is not my thought, I want to close the popup when click again. Anyone has idea to avoid this? Thanks for your help.

Sstx
  • 604
  • 1
  • 8
  • 21
  • you could to explain more, best could be in SSCCE form, because (as far as I understand) if is there JFrame as popup, then this is road to troubles – mKorbel Oct 24 '13 at 06:54
  • Take a look here: http://stackoverflow.com/questions/1900293/how-can-i-create-a-drop-down-menu-in-a-java-swing-toolbar The solution is theme dependent (It's implemented in BasicPopupMenuUI, so it should work if the theme is derived from BasicLookAndFeel). – kiheru Oct 24 '13 at 06:57
  • @mKorbel,why I use JFrame is because JFrame can gain the focus, but JWindow can't, the Date popup will allow user to input. – Sstx Oct 24 '13 at 07:27
  • @kleopatra Can you show me? – Sstx Oct 24 '13 at 07:28
  • @kleopatra you should know, the popup is not JPopupMenu, it is a JFrame which created by me, I can design it for adding some other controls. Since the code is not written by me, I just want to make sure there is a idea to fit my thought.The codes maybe not correct, just show me what I'm try. – Sstx Oct 24 '13 at 08:11
  • no, it's the other way round: _you_ show what you do and explain how it doesn't meet your expectation :-) Your question is missing essential details (f.i. where do you mention that you use a JFrame vs. a JPopupMenu and why), best to show a SSCCE. – kleopatra Oct 24 '13 at 08:16
  • @kleopatra I have written, I use a JFrame as a Date Popup. – Sstx Oct 24 '13 at 08:18
  • true, my bad reading :-) – kleopatra Oct 24 '13 at 08:23
  • Don't use a JFrame, use an undecorated JDialog. Then you can use a WindowListener and listen for windowDeactivated to close the dialog. – camickr Oct 24 '13 at 15:34

1 Answers1

0

You should follow the behavior that is already defined for the system. E.g. on Windows popups are opened on right click and closed on left click. Re-opening the popup for another right click is not a fault, it’s the default behavior here. Achieving the correct behavior regarding the system’s guidelines is easiest if not implemented by hand. Below is an example of a popup which is automatically opened and closed in a system-conforming way.

The popup shows a color chooser which allows to change the background of the textfield. Note how easy everything can be if you don’t do it by hand. The popup will also open if the user presses the Menu key on the keyboard, something you might not have thought about.

Use what others already have done for you…

import java.awt.BorderLayout;
import java.beans.EventHandler;

import javax.swing.*;
import javax.swing.event.ChangeListener;

public class Popups {
  public static void main(String[] args) {
    JFrame f=new JFrame("popup");
    JTextField tf = new JTextField(30);
    JPopupMenu popup = new JPopupMenu();
    JColorChooser cc = new JColorChooser();
    cc.getSelectionModel().addChangeListener(EventHandler.create(
        ChangeListener.class, tf, "background", "source.selectedColor"));
    popup.add(cc);
    tf.setComponentPopupMenu(popup);
    f.getContentPane().add(tf, BorderLayout.NORTH);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
  static {
    try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }
    catch(ClassNotFoundException | InstantiationException
        | IllegalAccessException | UnsupportedLookAndFeelException ex) {
      // if this fails life with the default L&F
    }
  }
}
Holger
  • 285,553
  • 42
  • 434
  • 765