Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a popup menu to a JTextField

Tags:

java

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).

like image 567
artaxerxe Avatar asked Dec 29 '22 08:12

artaxerxe


1 Answers

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?

like image 189
austen Avatar answered Jan 12 '23 12:01

austen