Reputation: 6421
Can anybody explain me how to add a popup menu at a JtextField? I managed to add a JPopupMenu:
JPopupMenu popup = new JPopupMenu();
popup.add("m");
popup.add("n");
JTextField textField = new JTextField();
textField.add(popup);
.....
But when i roll the mouse over "popup", nothing is happening (i need to select an item from popup).
Upvotes: 4
Views: 14994
Reputation: 3314
From your comment, it sounds like you are trying to display a sub-menu in the popup that appears over your JTextField.
// 1. Let's add the initial popup to the text field.
JTextField textField = new JTextField();
JPopupMenu popup = new JPopupMenu();
textField.add(popup);
textField.setComponentPopupMenu(popup);
// 2. Let's create a sub-menu that "expands"
JMenu subMenu = new JMenu("m");
subMenu.add("m1");
subMenu.add("m2");
// 3. Finally, add the sub-menu and item to the popup
popup.add(subMenu);
popup.add("n");
Hopefully I answered the question you are trying to ask. If not could you explain a bit more on what you are trying to accomplish?
Upvotes: 10