Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 940633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:59:17+00:00 2026-05-15T21:59:17+00:00

Alright, so heres my problem: I have an application I am working with that

  • 0

Alright, so heres my problem:

I have an application I am working with that creates a Dialog that has a JTree in it. when someone right clicks on the JTree I need to determine if its in the 2010 folder. If it is I need to create a popup menu with one option : “Migrate to 2011”.

I had am having now is that I have the menu pop up with the right text, and close when I click on it or when I click off. However, when I click on Migrate to 2011 nothing happens, it wont run my ActionPerformed, and makes a weird flashing on the top of the dialog window.

I created a new class to try to diagnos the problem, but am running into a new problem. This is the side class I made to try and figure out what was wrong, when I run it it populates fine and opens up the menuitem fine. However, when you click on the menu option it doesnt actually close the menu, instead it only runs the ActionPerformed method and nothing else.

Here is the code sample:

//Import statements

public class MainClass extends JPanel {

  public MainClass() {
    final JPopupMenu popup = new JPopupMenu();
    final JTree tree;
    final JTextField jtf;

    JMenuItem menuItem = new JMenuItem("Migrate to 2011");
    ActionListener actionlistener = new PopupActionListener();
    menuItem.addActionListener(actionlistener);
    popup.add(menuItem);

    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");

    DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
    top.add(a);
    DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
    a.add(a1);
    DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
    a.add(a2);

    DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
    top.add(b);
    DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
    b.add(b1);
    DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
    b.add(b2);
    DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
    b.add(b3);

    tree = new JTree(top);

    JScrollPane jsp = new JScrollPane(tree);

    add(jsp, BorderLayout.WEST);

    jtf = new JTextField("", 20);
    add(jtf, BorderLayout.EAST);

    tree.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent ae) {
        popup.setVisible(false);
        TreePath tp = tree.getPathForLocation(ae.getX(), ae.getY());
        if (tp != null)
          jtf.setText(tp.toString());
        else
          jtf.setText("");

        if(SwingUtilities.isRightMouseButton(ae)){
            popup.setLocation(ae.getLocationOnScreen());
            popup.setVisible(true);
        }   
      }
    });

  }

    class PopupActionListener implements ActionListener {
          public void actionPerformed(ActionEvent ae) {
            System.out.println("it works!");
          }
        }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 450);
    frame.setVisible(true);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:59:18+00:00Added an answer on May 15, 2026 at 9:59 pm

    The correct way to show JPopupMenu is to use its show method. Take a look at the API:

    In addition to that I suggest to use Actions instead of simple action listeners. They are more flexible and can be used not only on menus but toolbars etc. Here is something to read about actions:

    http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/misc/action.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, so heres my problem, i need my user to be able to upload
Alright, Heres the thing. I have a (paid for) shopping cart, which I am
I have a problem with setting up my Zend Framework application on live server.
Alright, here is my problem i'm trying to solve. I have an index page
Alright, so I'm working on a website and currently I have a shoutbox at
Alright, here we go. I have a cocos2d app, and there are targets that
Alright, so here's the dealio: I'm working on a Ruby app that'll take data
Alright, this problem seems to be way above my head! I have this code:
I have a document based application running with core data. The object model has
Alright, title is REALLY sloppy. Here's my problem: I have a news site and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.