java - How to select first item in JPopupMenu? -


In the past, when one made a JPopupMenu Visible This item will be selected by default:

Today, the default behavior is to pop up the menu without any items I will be selecting and pop up centered under a would make JPopupMenu which the mouse pointer with a code. I before has managed to get centered pop up at the bottom of the mouse but i Jemenuitm declined to render the like that has been decided that I move the mouse out of the item and right again From

Any idea

Here's my testcase to choose:.

  Import Java .awt.Component; Import java.awt.Point; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import java.awt.event.MouseAdapter; Import java.awt.event.MouseEvent; Import javax.swing.JFrame; Import javax.swing.JMenuItem; Import javax.swing.JPopupMenu; Public Test Test Jeffrey {Public Static Expands Zero Main (String [] Args) {JFrame frame = new JFrame (); Frame.setSize (800, 600); . Frame.getContentPane () addMouseListener (New MouseAdapter () {@Override public void mousePressed (MouseEvent e) {if (e.isPopupTrigger ()) popupTriggered (e);} @Override public void mouseReleased (MouseEvent e) {if (e. isPopupTrigger ()) popupTriggered (e);} private void popupTriggered (MouseEvent e) {JPopupMenu menu = new JPopupMenu (); final JMenuItem item = new JMenuItem ( "this is a JMenuItem"); menu.add (item); point point = e.getPoint (); int x = point.x - (item.getPreferredSize () width / 2); int y = point.y - (item.getPreferredSize () height / 2); Menukshow (( Component) e.getSource (), x, y);}}); Frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE); Frame.setVisible (true); }}  

Nowadays default behavior to pop up a menu without any Is the item selected

Actually, I used to argue that this is the right behavior, at least in Windows, other non-Java applications also do this, I do not think it is worth breaking the conference , Even if there are only one item in the menu. If you feel otherwise, you can set the selection index to this form.


Therefore, I finally got an opportunity to see it on Java 1.6.0_11 , and found some incompatible behavior: If the popup menu exits from the original frame, then The item is automatically selected; If the popup menu appears completely in the original frame, then nothing is selected. Sounds like a swing bug, which responds to an RFR for at least continuous behavior.


Comments