Results 1 to 3 of 3
- 01-11-2012, 06:31 AM #1
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Problem with JMenu, JPopupMenu [HELP]
I am having a problem with JPopupMenu.
I create a simple JTree. when the right to display a JPopupMenu of JTree.
When the JPopupMenu is show up again I want to click on a JTree node then takes two clicks of the mouse select the node. If you remove this line "UIManager.setLookAndFeel (UIManager.getSystemLookA ndFeelClassName ());" then click only once.
If you want to use "UIManager.setLookAndFeel (UIManager.getSystemLookA ndFeelClassName ());"
How they must.
Java Code:import static java.awt.event.InputEvent.CTRL_DOWN_MASK; import java.awt.*; import java.awt.event.*; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.*; import javax.swing.tree.*; public class TreePopup { JTree tree = new JTree(); JPopupMenu popup; public TreePopup() { popup = new JPopupMenu(); popup.setInvoker(tree); PopupHandler handler = new PopupHandler(tree, popup); popup.add(getMenuItem("Add child", handler, 'I')); popup.add(getMenuItem("Add sibling", handler, 'M')); popup.addSeparator(); popup.add(new JMenuItem("New")); popup.add(new JMenuItem("Open")); popup.add(new JMenuItem("Copy")); popup.add(new JMenuItem("Cut")); } private JMenuItem getMenuItem(String s, ActionListener al, char c) { JMenuItem menuItem = new JMenuItem(s); menuItem.setAccelerator(KeyStroke.getKeyStroke(c, CTRL_DOWN_MASK)); menuItem.setActionCommand(s.toUpperCase()); menuItem.addActionListener(al); return menuItem; } private JScrollPane getTreeComponent() { tree.add(popup); expand(new TreePath(tree.getModel().getRoot())); return new JScrollPane(tree); } @SuppressWarnings("unchecked") private void expand(TreePath path) { TreeNode node = (TreeNode) path.getLastPathComponent(); if (node.getChildCount() >= 0) { java.util.Enumeration e = node.children(); while (e.hasMoreElements()) { TreeNode n = (TreeNode) e.nextElement(); expand(path.pathByAddingChild(n)); } } tree.expandPath(path); } public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); TreePopup test = new TreePopup(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getTreeComponent()); f.setSize(400, 400); f.setLocation(200, 200); f.setVisible(true); } } class PopupHandler implements ActionListener { JTree tree; JPopupMenu popup; Point loc; public PopupHandler(JTree tree, JPopupMenu popup) { this.tree = tree; this.popup = popup; tree.addMouseListener(ma); } public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); TreePath path = tree.getPathForLocation(loc.x, loc.y); // System.out.println("path = " + path); // System.out.printf("loc = [%d, %d]%n", loc.x, loc.y); if (ac.equals("ADD CHILD")) addChild(path); if (ac.equals("ADD SIBLING")) addSibling(path); } private void addChild(TreePath path) { DefaultMutableTreeNode parent = (DefaultMutableTreeNode) path .getLastPathComponent(); int count = parent.getChildCount(); DefaultMutableTreeNode child = new DefaultMutableTreeNode("child " + count); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.insertNodeInto(child, parent, count); } private void addSibling(TreePath path) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) path .getLastPathComponent(); DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node .getParent(); int count = parent.getChildCount(); DefaultMutableTreeNode child = new DefaultMutableTreeNode("child " + count); DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); model.insertNodeInto(child, parent, count); } private MouseListener ma = new MouseAdapter() { private void checkForPopup(MouseEvent e) { if (e.isPopupTrigger()) { loc = e.getPoint(); popup.show(tree, loc.x, loc.y); } } public void mousePressed(MouseEvent e) { checkForPopup(e); } public void mouseReleased(MouseEvent e) { checkForPopup(e); } public void mouseClicked(MouseEvent e) { checkForPopup(e); } }; PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { @Override public void propertyChange(PropertyChangeEvent evt) { } }; }
- 01-11-2012, 12:08 PM #2
Re: Problem with JMenu, JPopupMenu [HELP]
You have omitted one vital detail: since the problem arises only with the SystemLookAndFeel, you need to tell us what system(s) you have tested this on (Windows/Windows classic/Linux/Mac).
Add a line after setLookAndFeelReport here what that prints on your system.Java Code:System.out.println(UIManager.getLookAndFeel());
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-12-2012, 04:56 AM #3
Member
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Re: Problem with JMenu, JPopupMenu [HELP]
result line code
[The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel]Java Code:System.out.println(UIManager.getLookAndFeel());
Detail
thankJava Code:public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { System.out.println(UIManager.getLookAndFeel()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); System.out.println(UIManager.getLookAndFeel()); //TreePopup test = new TreePopup(); //JFrame f = new JFrame(); //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //f.add(test.getTreeComponent()); //f.setSize(400, 400); //f.setLocation(200, 200); //f.setVisible(true); } RESUTL [The Java(tm) Look and Feel - javax.swing.plaf.metal.MetalLookAndFeel] [The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel]Last edited by longtqdayma; 01-12-2012 at 05:02 AM.
Similar Threads
-
JMenu on Right side problem
By justbeller in forum AWT / SwingReplies: 2Last Post: 03-05-2011, 05:11 PM -
problem with jmenu,please help!!!
By beni.vd in forum AWT / SwingReplies: 4Last Post: 12-31-2010, 07:08 PM -
Applet in JFrame, JMenu problem
By Bill86 in forum New To JavaReplies: 5Last Post: 12-28-2009, 04:20 AM -
JMenu to JPopupMenu
By carderne in forum New To JavaReplies: 0Last Post: 02-28-2009, 06:07 PM -
JPopupMenu problem
By sylvievdb in forum AWT / SwingReplies: 3Last Post: 10-06-2008, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks