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);
}
}