views:

36

answers:

2

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  A: 

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

eugener
+1  A: 

Use mouseReleased event in mouseAdapter

public void mouseReleased(MouseEvent ae) { 
   if(ae.isPopupTrigger()){
       popup.show(ae.getComponent(),ae.getX(),ae.getY());
   }
    TreePath tp = tree.getPathForLocation(ae.getX(), ae.getY()); 
    if (tp != null) 
      jtf.setText(tp.toString()); 
    else 
      jtf.setText(""); 
  } 
harshit