How to add popup menu to JTextField

Can someone explain to me how to add a popup menu to JtextField? I managed to add JPopupMenu:

JPopupMenu popup = new JPopupMenu();
    popup.add("m");
popup.add("n"); 

JTextField textField = new JTextField();
textField.add(popup);

.....

But when I turn the mouse over the "popup", nothing happens (I need to select an item from the popup).

+3
source share
4 answers

From your comment, it looks like you are trying to display a submenu in the popup that appears above your JTextField.

// 1. Let add the initial popup to the text field.
JTextField textField = new JTextField();
JPopupMenu popup = new JPopupMenu();
textField.add(popup);
textField.setComponentPopupMenu(popup);

// 2. Let 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");

I hope I answered the question you are trying to ask. If you could not explain a little more about what you are trying to accomplish?

+9
source

I don’t think it is as simple as this code looks. You can take a look at this example

0

, .

0
source

Read the JComponent API for the method setComponentPopupMenu().

0
source

Source: https://habr.com/ru/post/1725811/


All Articles