Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By jim829

Thread: JPopupMenu disappears on mouseEntered. How to prevent ?

  1. #1
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Question JPopupMenu disappears on mouseEntered. How to prevent ?

    Hello All,

    I've a swing structure : JPopupMenu contains JPanel which contains JCheckBoxMenuItem. The problem is, I cannot use it because JPopupMenu disappears when mouse enters to any MenuItems. Popup should be open so that user can check/uncheck multiple CheckBox.
    I added JCheckBoxMenuItem to JPanel(because of scrollable, as there are large number of menu items) and then JPanel to JPopupMenu.
    I'm using NetBeans IDE.
    Here is the code, done so far.

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            int count = 250; // this count value must be dynamic
            JPopupMenu menu = new JPopupMenu();
            JCheckBoxMenuItem item = null;
            JPanel panel = new JPanel();
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            JScrollPane scrollPane = new JScrollPane(panel,
                    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setPreferredSize(new Dimension(125, 200));
            for (int i = 1; i <= count; i++) {
                item = new JCheckBoxMenuItem("Page : " + i, new ImageIcon("test.png"));
                item.setHorizontalTextPosition(JMenuItem.RIGHT);
                item.addActionListener(new OpenAction(menu, jButton1));
                panel.add(item);
            }
            menu.add(scrollPane);
    
            if (!menu.isVisible()) {
                Point p = jButton1.getLocationOnScreen();
                menu.setInvoker(jButton1);
                menu.setLocation((int) p.getX(), (int) p.getY() + jButton1.getHeight());
                menu.setVisible(true);
            } else {
                menu.setVisible(false);
            }
        }
    Please help. Thanks in advance.
    Last edited by TempExpt; 03-07-2014 at 08:37 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    Please submit a Short, Self Contained, Correct Example that demonstrates the problem. Probably less than 70 lines of code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    The problem is, I cannot use it because JPopupMenu disappears when mouse enters to any MenuItems
    I have never seen that behaviour.

    Popup should be open so that user can check/uncheck multiple CheckBox.
    That is a different issues because a popup is designed to close once an item has been selected. However, you can check out: 2010 September 12 « Java Tips Weblog /keeping-menus-open/ for a solution.

  4. #4
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    Hi camickr: Thanks for your reply. But the fact is JPopupMenu disappears on mouseEnter. Could you please confirm what is the wrong with the code ?

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    You have been asked to post a SSCCE. You didn't so we can't help.

  6. #6
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    This is a single piece of code of my entire project. I'm stuck in this portion. That's why I've posted this portion. Please check this code & inform what's wrong with this.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    That is insufficient. It should not be hard to write a small program duplicating the part of your code that doesn't work. In fact, in doing it you may solve the problem yourself. And when writing complicated software it is good practice to ensure that various pieces work as expected before integrating them into the main project.

    Regards,
    Jim
    Last edited by jim829; 03-08-2014 at 07:03 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?


  9. #9
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class PopupTest extends JFrame {

    JButton button1;

    public PopupTest() {
    setTitle("Popup Test !");
    setSize(400, 400);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    button1 = new JButton("Click me!");
    button1.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
    int count = 250; // this count value is dynamic
    JPopupMenu menu = new JPopupMenu();
    JCheckBoxMenuItem item = null;
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    JScrollPane scrollPane = new JScrollPane(panel,
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setPreferredSize(new Dimension(125, 200));
    for (int i = 1; i <= count; i++) {
    item = new JCheckBoxMenuItem("Page : " + i, new ImageIcon("test.png"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    panel.add(item);
    }
    menu.add(scrollPane);

    if (!menu.isVisible()) {
    Point p = button1.getLocationOnScreen();
    menu.setInvoker(button1);
    menu.setLocation((int) p.getX(), (int) p.getY() + button1.getHeight());
    menu.setVisible(true);
    } else {
    menu.setVisible(false);
    }
    }
    });
    add(button1);
    }

    public static void main(String args[]) {
    new PopupTest();
    }
    }


    This is the entire code.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,034
    Rep Power
    6

    Default Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    To solve your immediate problem, remove the men.setInvoker() statement.

    Now, you need to do some other basic things.

    First, take all the intialization stuff out of your actionPerformed method. You don't need to reinitialize the menu everytime it is pressed.
    Second, don't extend JFrame. Just create a instance and add the components to it.

    Your actiionPerformed method should look like this.

    Java Code:
    public void actionPerformed(ActionEvent e) {
        if (!menu.isVisible()) {
            Point p = button1.getLocationOnScreen();
            menu.setLocation((int) p.getX(), (int) p.getY() + button1.getHeight());
            menu.setVisible(true);
        } else {
            menu.setVisible(false);
        }
    }
    Regards,
    Jim
    TempExpt likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    TempExpt is offline Member
    Join Date
    Mar 2014
    Posts
    13
    Rep Power
    0

    Thumbs up Re: JPopupMenu disappears on mouseEntered. How to prevent ?

    Thank you jim, thanks a lot. It's working fine.

Similar Threads

  1. jDialog box disappears
    By pink123 in forum AWT / Swing
    Replies: 3
    Last Post: 08-19-2011, 07:07 PM
  2. MouseEntered to rect
    By g6pd in forum New To Java
    Replies: 3
    Last Post: 03-10-2011, 02:52 PM
  3. moving a pic, another pic disappears
    By benjamin in forum New To Java
    Replies: 4
    Last Post: 06-16-2010, 07:18 PM
  4. can i set a timer delay on mouseEntered event
    By chan in forum AWT / Swing
    Replies: 1
    Last Post: 08-30-2009, 06:55 AM
  5. Detecting mouseEntered event in JComponent
    By djhallx in forum AWT / Swing
    Replies: 1
    Last Post: 02-26-2009, 11:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •