Problem dismissing a JPopupMenu

I have a DropDownCalendar class that extends JButton. I am inserting a calendar(JPanel) into a JPopupMenu. The JPopupMenu is then invoked by the user when they click on the DropDownCalendar button. When a user clicks on a date in the calendar, the dateChanged() action is called which sets the selected date and then hides the JPopupMenu. After I hide the JPopupMenu, I then have to click TWICE on the DropDownCalendar button before the mouse event is fired again to redisplay the popup. The button never receives the first mouse click, but then receives the second. I think the popup is grabbing the first mouse click. Is popup.setVisible(false) the right way to dismiss the popup?
Here's the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.util.*;
public class DropDownCalendar extends JButton {
     JLabel          dateLabel, arrowLabel;
     JPopupMenu jpm;
     SimpleCalendar scal;
     SimpleDateFormat dateFormatter;
     Dimension origin = new Dimension(0, 0);
     class MouseHandler extends MouseAdapter {
          public void mousePressed(MouseEvent me) {
               jpm.show(me.getComponent(), 0, me.getComponent().getHeight());
* DropDownCalendar constructor comment.
public DropDownCalendar() {
     super();
     // Set display format to "Mon, Dec 31, 2001"
     dateFormatter = new SimpleDateFormat ("EEE, MMM d, yyyy");
     // Button contains two JLabels. One for the date string and another
     // for the up/down arrow.
     dateLabel = new JLabel("", JLabel.LEFT);
     arrowLabel = new JLabel(" \u25BC", JLabel.RIGHT);
     setMargin(new Insets(0,0,0,0));
     setLayout(new BorderLayout());
     add(dateLabel, BorderLayout.WEST);
     add(arrowLabel, BorderLayout.EAST);
     addMouseListener(new MouseHandler());
     scal = new SimpleCalendar();
     scal.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
     dateChanged();
     jpm = new JPopupMenu();
     jpm.setBorderPainted(false);
     jpm.add(scal);
     dateChanged();     // Trigger this to initialize the date string in the button
private void dateChanged()
     // Set the date string the in the button
     dateLabel.setText(dateFormatter.format(scal.getSelection()));
     jpm.setVisible(false);

I am having the same problem. If the JPopupMenu is visible and the user clicks somewhere else on the JPanel to select something else, the JPopupMenu goes away but the MouseEvent doesn't get called on the JPanel. The only MouseEvent I get is mouseEntered. I put a PopupMenuListener on the JPopupMenu and tried requesting focus and transfer focus to my JPanel but that doesn't work. I have noticed that it does work if the same code it applied to a JTable or JTree?? On a JTable after the popupMenuWillBecomeInVisible a mouseReleased event is fired on the JTable and it looks like it is coming from java.awt.LightweightDispatcher.retargetMouseEvent. How do I get this to work just using a JPanel?

Similar Messages

  • Problem in Opening JPopupMenu

    Hi
    We are facing a problem in opening JPopupMenu in multiple browser windows. We have a parent window which has a JApplet. We have another window which is a child window of the parent. That also has a different applet.
    When we right click on the applet which is available in the parent window, the popup menu shows up.
    When we do the same on the child window, the popup shows up finely. Now when we select any menu item in the child window's popup, the focus goes to the parent window and then immediately comes back to the child window.
    We are not overriding any focus lost event in the applets or handling any key events. We are using JDK 1.4.1 plugin to the browser.
    I appreciate your suggestion/comments.
    Thanks,
    Murugu

    I will resate the problem described earlier.
    We have an applet which is displayed in 2 browser instances (parent/child) windows. The 2 windows have different instances of the same applet.
    Problem : When the user right clicks on the applet container, a popup menu is displayed. Keeping the popup menu display intact, when the user moves to the child window and right clicks on the applet container to display the popup menu, slight flickering happens which is to dispose the popup menu displayed in the first applet and then displays in the second applet.
    In the mouseReleased() method in the applet we are creating a new instance of JPopupMenu adding the necessary menu items and then displaying it. We have made sure that the 2 browser windows are having 2 different instances of the applet.
    Does this mean that the JVM always creates one instance of the popup menu ?? OR how to avoid this flickering problem ??
    Further to the problem mentioned above :
    1. The user right clicks on the first applet to display the popup menu.
    2. Clicks on a menu item (Currently no event is fired when the menu is selected). The popup menu will be      disposed after this operation.
    3. The user then moves to the second applet and right clicks on the second applet to display the popup menu.
    4. Now when the user presses the mouse button on the menu item, there is a focus shift from the second applet to the first applet. And when the mouse is released the focus comes back to the second applet.
    Note : There is no actionlistener is added to the menu items.
    Any one has any idea why this kind of behaviour is occurring in the applet and popup menu ??

  • Very Urgent - Problem in showing JPopupMenu using 1.4 plugin

    Hello ,
    I have a very different problem with JPopupMenu. I have developed a new PopupMenu extending the MetalComboPopup used by the MetalComboBoxUI. I have registered with the JPopupMenu for the callback methods popupMenuWillBecomeVisible && Invisible(). I have an applet which instantiates (3 instances) my JComboBox. When I run the applet using the JDK1.4 plugin, when I click the second JComboBox, the first and the second JComboBox's popups appear at the same time. I am unable to suppress the popup (i.e Only one popup should appear at a time).
    Can anybody help me out in this issue.
    If you have a solution you can email to me -> [email protected] /
    [email protected]
    Thankx in advance,
    SS

    Did you try changing the driver then?
    Please use Oracle's thin driver instead of the oci driver.
    There are many advantages of using the type 4 driver. the first and foremost being that it does not require oracle client side software on your machine. Therefore no enteries to be made in tnsnames.ora
    The thin driver is available in a jar called classes112.zip the class which implements the thin driver is oracle.jdbc.driver.OracleDriver
    the connection string is
    jdbc:oracle:thin:@<machine name>:1521:<sid>
    please try out with the thin driver and let me know.
    regards,
    Abhishek.

  • JPopupMenu - Problem with Handling ESC character.

    As per the Javadoc, it says ESC is the key for retracting the PopupMenu. But it is not retracting whenever we press ESC.
    Is this a bug?
    If it is not a bug, how do i retract the popup menu when the Esc is pressed.
    Thanks,
    -Shenbugs

    I also have my JDialogs set up to close when ESC is pressed, by overriding the createRootPane() method:  protected JRootPane createRootPane()
        closeDialogAction = new CloseDialogAction();
        JRootPane rootPane = new JRootPane();
        KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        rootPane.registerKeyboardAction(closeDialogAction, stroke,
            JComponent.WHEN_IN_FOCUSED_WINDOW);
        return rootPane;
      }I tried adding a menubar to one of my dialogs (which I had never done before), and I saw the same problem as you: if I pressed escape when the menu was open, the whole dialog disappeared. But if I comment out the method above and recompile, the menubar works as it should.
    The problem is that JPopupMenu's InputMap uses a WHEN_IN_FOCUSED_WINDOW binding, which appears to be the binding of last resort (I tried the above with a WHEN_ANCESTOR_OF_FOCUSED_COMPONENT binding, with the same results). So, while we would like ESC to be interpreted as "if a menu is open, close it; otherwise close the dialog", that doesn't seem to be possible.
    In that dialog's RootPane i added a InputMap for "ESC"
    key to map that to WindowClosingEvent. And while
    running, if the popupMenu is shown, and Esc is pressed
    the Dialog is closed. And if i remove that ActionMap,
    popupMenu still remains visible though i press ESC.You say you removed the ActionMap, but did you remove the InputMap as well? If not, your RootPane is probably still consuming the ESC-key event.

  • Problems with JPopupMenu

    Hi :
    I have a problems with a JPopupMenu.
    I have a JTable in a modal JDialog with some rows and I want to make some action with some rows so I make JPopupMenu visible with right click (no problem) but, when it reachs the JDialog border, it appears cut because it can't paint out of JDialog limits.
    What can I do to watch the entire JPopupMenu ? It's urgent.
    Thanks.
    Miquel

    This is because the Popupmenu is a lightweight component (which can't display beyond the borders of the window), try myPopup.setLightWeightPopupEnabled(false); that makes all popups in your app heavyweight.

  • Cannot dismiss iOS 8 keyboard

    Has anyone else had a problem dismissing the iPad's iOS 8 keyboard when you have a Bluetooth keyboard attached?
    From time to time I run into a problem dismissing the iOS 8 keyboard when the cursor is in a text field, even though I have a Bluetooth keyboard connected.
    This is an iPad Air with a Logitech keyboard. I've tried various "fixes" but nothing seems to work.
    Turning the Logitech keyboard off
    Cycling the Logitech keyboard off and back on
    Turning off Bluetooth and turning it back on
    Returning to the homescreen to swipe down and cancel the spotlight search
    The only thing that seems to work right now is powering the iPad off and rebooting. This works for a while and then, for no apparent reason, the problem will reappear.
    Has anyone else had this problem and what solution(s) have you found?

    I'm surprised no one else has posted about this problem.  I have the same issue.  It happened to me yesterday.  Only the keyboard popped up on the springboard.  Apparently the last thing I did there was log into iCloud because the text that appeared in the little helper boxes was my iCloud password, in the clear. 

  • Netbeans and JPopupMenu

    Im having a problem creating a JPopupMenu,
    Im using the netbeans GUI builder, I've added the popup menu to the Jframe, and can get it to display itself when right clicked, however i cannot get it to hold any components.
    I tried adding a menu item to it, first by dragging and dropping, which did not work, then by right clicking and adding from pallete, which did work.
    However, the menu item is not diplayed.
    I tried adding various other containers too, but they are not displayed either, any ideas what im doing wrong?
    Thanks,
    James

    Yes quite, netbeans is absolutley fine, frankly, the java website suggests using it for gridbag... Some people just like to feel 1337.
    Curiously its working now, kinda.
    There seems to be a bug of somesort that causes the palette to empty, at which point i have to recreate the project and it comes back, since then however, the popupmenu seems to work, although the offset is a bit wonky
    jPopupMenu1.setLocation(evt.getX(), evt.getY());
                jPopupMenu1.setEnabled(true);
                jPopupMenu1.setVisible(true); that should put the menu where the mouse was at the time, however it seems to be off slightly, any ideas why?

  • Clearing a JPopupMenu

    Hi all!
    I've got a problem with a JPopupMenu! I made a JPopupMenu with 5 different contents. So my question is:
    How could i clear a JPopupMenu so i can put the new content into it?
    Regards,
    Jasper Kouwenberg

    Ya, i tryed but the content is different every time
    so i don't know how much index's there are :SYou could do:
    while(true){
    try{
    mymenu.remove(0);
    catch(IllegalArgumentException e){
    break;
    }

  • JPopupMenu not disappearing in Mac OS 10.6

    Hello, I am experiencing a problem with a JPopupMenu on Mac 10.6, once the menu is active a switch in the active application doesn't make it disappear it is shown above the other active windows, the strange thing is that this behavior is particular on Mac 10.6. On Windows and on Mac 10.5 it works completely fine.
    Greetings
    Jose Luis Vega

    Thanks Luca, the problem is that the listeners are not triggered while another aplication is active the event received was when the popup is active and then nothing. Look the issue was shown on popus that are triggered by a right click, while the java application is not active so the popup is shown 'above' other applications which is a normal mac behavior the problem is that once the popup is there you can turn to the other applicatios and the popup remains open and above everithing on the screen. I believe is a java implementation bug on mac but I don't know how to fix it because as I said no events are triggered from the menu or the parent components.

  • PopupMenu behavior

    Hi all!
    I have a problem with the JPopupMenu behavior. My popup menu has a submenu:
    JPopupMenu p = new JPopupMenu();
    JMenu menu = new JMenu();
    menu.add(new JMenuItem("1"));
    menu.add(new JMenuItem("2"));
    p.add(menu);
    p.setVisible(true);
    It works, but it doesn't have the 'normal' menu behavior, like the ones in a JMenuBar. I mean when I click on the submenu, the components don't popup. Is there a way to change this with a specific method ?
    Thanks!

    Hello
    Try this:
    JTextArea text = new JTextArea(10,20);
    JMenu menu = new JMenu("Menu");
    JPopupMenu popup = new JPopupMenu();
    JMenuItem mnitm1 = JMenuItem("1");
    JMenuItem mnitm2 = JMenuItem("2");
    menu.add(mnitm1);
    menu.add(mnitm2);
    popup.add(menu);
    // add mouse listener to pop up menu
    MouseListener popuplistener = new popupListener();
    text.addMouseListener(popuplistener);
    class popupListener extends MouseAdapter {
    public void mousePressed(MouseEvent e) {
    showPopup(e);
    try{
    }catch(Exception ex){
    public void mouseReleased(MouseEvent e) {
    showPopup(e);
    private void showPopup(MouseEvent e) {
    if (e.isPopupTrigger()) {
    popup.show(e.getComponent(),
    e.getX(), e.getY());
    This will work!!!!
    Rana

  • Problem with  JTree and JPopupMenu

    Hi,
    I'm using a JPopupMenu with a JPanel in it. In the panel
    I want to have a JTree. I can build the tree without
    problems but when I try to "go down" in the tree it
    vanishes. This happens only the first time that I
    access a node. When I initialize the menu a second
    time the node I just pressed works fine.
    What is the problem????
    Here is a sample of my code.
    popUpMenu = new JPopupMenu();
    thePanel = new JPanel();
    thePanel .setLayout(new GridBagLayout());
    popUpMenu .add(thePanel );
    DefaultMutableTreeNode top = new DefaultMutableTreeNode("The top");
    DefaultMutableTreeNode secondNode = null;
    DefaultMutableTreeNode firstNode = null;
    firstNode = new DefaultMutableTreeNode("One node");
    top.add(firstNode);
    secondNode= new DefaultMutableTreeNode("One node");
    firstNode.add(secondNode);
    buildConstraints(gbc, 1, 0, 1, 5, 5, 5, 5, 2, 1); //My contraintsmethod
    JTree tree = new JTree(top);
    thePanel .add(tree, gbc);

    Mate, why are you putting a JPanel in a JPopupMenu? I'd be interested to look at a screenshot of this.
    Mitch

  • Problem with InputVerifier and JPopupMenu

    Hello, I have a problem related to the usage of InputVerifier and JPopupMenu.
    Consider the following scenario:
    - A frame composed of two components: a JTextField and a JComboBox
    - An InputVerifier that was set in the JComboBox (and in its associated JTextComponent)
    - A JPopupMenu which is enabled by a right mouse click over the components of the frame.
    The verify() method of the InputVerifier class is executed every time the focus is on the JComboBox and is requested by other component.
    Suppose that the focus is on the JComboBox, and a right mouse click is executed over this components. In summary, we have:
    1) The method verify() of the InputVerifier associated to the JComboBox is executed
    2) The focus changes from the JTextComponent (associated to the JComboBox) to the JRootPane (associated to the JTextComponent (that is associated to the JPopupMenu)).
    Now suppose that, the user clicks in the JTextField component (note that the JPopupMenu is visible and has the focus). What happens is that the focus goes from the JRootPane -> JTextComponent (associated to the JComboBox) -> JTextField.
    Despite the focus flow through the JTextComponent, the Swing plattaform, when executing the runInputVerifier() in the JComponent context, get the JRootPane component as the focusOwner instead of the JTextComponent.
    In this way, the Swing platform tries to invoke the InputVerify of the JRootPane (that is set with "null"), and the JComboBox InputVerifier is not executed as expected.
    It works as if the focus is flowing from the JRootPane directly to the JTextField.
    However, if the user clicks on the JComboBox before clicking in the JTextField, the Swing platform executes the correct InputVerifier, i.e., the one associated to the JComboBox (actually, the one associated to the JTextComponent).
    Any thoughts to solve this problem?
    Thanks

    A plus information, I am using jdk1.5.0_22.

  • Jpopupmenu visibility problem with JWindows

    Hello,
    BACKGROUND:
    I am attempting to implement a feature similar to one found in the netbeans IDE for a programming editor I am helping to write. The feature is an autocomplete/function suggestion based on the current word being typed, and an api popup for the selected function.
    Currently a JPopupMenu is used to provide a list of suggested functions based on the current word being typed. EG, when a user types 'array_s' a JPopupMenu pops up with array_search, array_shift, array_slice, etc.
    When the user selects one of these options (using the up/down arrow keys) a JWindow (with a jscrollpane embedded in it) is made visible which displays the api page for that particular function.
    PROBLEM:
    The problem is that when a user scrolls down the JWindow the JPopupmenu disappears so he user cannot select another function.
    I have added a ComponentListener to the JPopupMenu so that when componentHidden is called I can do checks to see if it should be visible and make visible if necessary. However, componentHidden is never called.
    I have added a focuslistener to the JPopupMenu so that when it loses focus I can do the same checks/make visible if necessary. This function is never called.
    I have added a popupMenuListener but this tells me when it is going to make something invisible, not actually when it's done it, so I can't call popup.setVisible(true) from popupMenuWillBecomeInvisible because at that point the menu is still visible.
    Does anyone have any suggestions about how I can scroll through a scrollpane in a JWindow whilst still keeping the focus on a separate JPopupMenu in a separate frame?
    Cheers

    The usual way to do popup windows (such as autocomplete as you're doing) is not to create a JPopupMenu, but rather an undecorated (J)Window. Stick a JList in the popup window for the user to select their choice from. The problem with using a JPopupMenu is just what you're experiencing - they're designed to disappear when they lose focus. Using an undecorated JWindow, you can control when it appears/disappears.
    See this thread for information on how to do this:
    http://forum.java.sun.com/thread.jspa?threadID=5261850&messageID=10090206#10090206
    It refers you to another thread describing how to create the "popup's" JWindow so that it doesn't steal input focus from the underlying text component. Then, further down, it describes how you can forward keyboard actions from the underlying text component to the JWindow's contents. This is needed, for example, so the user can keep typing when the autocomplete window is displayed, or press the up/down arrow keys to select different items in the autocomplete window.
    It sounds complicated, but it really isn't. :)

  • Flickerring Problem With JPopUpMenu

    Hello Friends,
    In my application made in Java i show a JPopUpmenu on a JINternalFrame in a DekstopPane which is further kept in JFrame. The contents of the PopUpMenu may vary as according to the various places where the mouse is clicked. Sometimes when the mouse may be clicked on the lower part of the applciation some part of the PopUpmenu goes out of the visible area of the screen. For that after showing the popupmenu i get the height of the popup menu and shift it accordingly so that its length doesnt go out of the viewable area. It works fine but the problem is that it gives a little flicker. And alsoo of i try to set the position before showing the pop up menu i am not able to do so because before calling the show() method of the popUPmenu i am getting the height and width of the pop up menu. Please help me out.
    Any type of pointers and links would be highly helpful.
    Thanks in Advance
    Vikram.

    HI!
    it didnt work
    here is the code snippet
              popupClass.pack();
              popupClass.requestFocus();          
              java.lang.System.out.println(" HEIGHTTTTT "+popupClass.getHeight());
              int Height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
              int Width = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
              if ((DiagramInternalPane)getParent() != null)
                   if(((mouseY+popupClass.getHeight()-((DiagramInternalPane)getParent()).GetParent().scrollpane.getVerticalScrollBar().getValue()+AsterixApplet.getFrame().getY()) > Height-160) && ((mouseX+popupClass.getWidth()-((DiagramInternalPane)getParent()).GetParent().scrollpane.getHorizontalScrollBar().getValue()+AsterixApplet.getFrame().getX()) > Width-20))
                        popupClass.show(comp,mouseX-popupClass.getWidth(), mouseY-popupClass.getHeight());
                   else
                        if((mouseY+popupClass.getHeight()-((DiagramInternalPane)getParent()).GetParent().scrollpane.getVerticalScrollBar().getValue()+AsterixApplet.getFrame().getY()) > Height-160)
                             popupClass.show(comp,mouseX, mouseY-popupClass.getHeight());
                        if((mouseX+popupClass.getWidth()-((DiagramInternalPane)getParent()).GetParent().scrollpane.getHorizontalScrollBar().getValue()+AsterixApplet.getFrame().getX()) > Width-20)
                             popupClass.show(comp,mouseX-popupClass.getWidth(), mouseY);     
              if(getParent() != null)     
                   getParent().repaint();
              popupClass.show(comp,mouseX, mouseY);                
              java.lang.System.out.println(" HEIGHTTTTT AAAAAAAAAAAAAAAA"+popupClass.getHeight());
    Before caluing the show() , method i am getting the height as 0 and only after calling the show method i am getting the actual height. Is it a java bug or something ?
    Anyway thanks for all ur help

  • I had no problem connecting to my network and now all of a sudden I can't connect.  Instead of prompting me for a password it comes back and says unable to join the network ("name") and then the option is Dismiss.  Anyone else have that problem?

    I had no problem connecting to my network and now all of a sudden I can't connect my iPad.  Instead of prompting me for a password it comes back and says unable to join the network ("name") and then the option is Dismiss.  Anyone else have that problem?  I've gone thru all my settings and can't seem to find a way to log into my network.  Don't get the option to put in my password.  It just give me the name of my network and option of dismiss instead of password.

    1. Turn router off for 30 seconds and on again
    2. Settings>General>Reset>Reset Network Settings

Maybe you are looking for