Results 1 to 3 of 3
  1. #1
    longtqdayma is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Red face 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) {
    
    		}
    	};
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default 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 setLookAndFeel
    Java Code:
    System.out.println(UIManager.getLookAndFeel());
    Report here what that prints on your system.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    longtqdayma is offline Member
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem with JMenu, JPopupMenu [HELP]

    result line code
    Java Code:
     System.out.println(UIManager.getLookAndFeel());
    [The Microsoft Windows Look and Feel - com.sun.java.swing.plaf.windows.WindowsLookAndFeel]

    Detail
    Java 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]
    thank
    Last edited by longtqdayma; 01-12-2012 at 05:02 AM.

Similar Threads

  1. JMenu on Right side problem
    By justbeller in forum AWT / Swing
    Replies: 2
    Last Post: 03-05-2011, 05:11 PM
  2. problem with jmenu,please help!!!
    By beni.vd in forum AWT / Swing
    Replies: 4
    Last Post: 12-31-2010, 07:08 PM
  3. Applet in JFrame, JMenu problem
    By Bill86 in forum New To Java
    Replies: 5
    Last Post: 12-28-2009, 04:20 AM
  4. JMenu to JPopupMenu
    By carderne in forum New To Java
    Replies: 0
    Last Post: 02-28-2009, 06:07 PM
  5. JPopupMenu problem
    By sylvievdb in forum AWT / Swing
    Replies: 3
    Last Post: 10-06-2008, 08:37 AM

Posting Permissions

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