artaxerxe
artaxerxe

Reputation: 6421

How to add a popup menu to a JTextField

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

Answers (4)

camickr
camickr

Reputation: 324207

Read the JComponent API for the setComponentPopupMenu() method.

Upvotes: 1

austen
austen

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

Boris Pavlović
Boris Pavlović

Reputation: 64650

Maybe editable combo box could suit you better.

Upvotes: 0

Varun
Varun

Reputation: 1004

I don't think its as straightforward as the code in question looks. You may want to have a look at this example

Upvotes: 0

Related Questions