tags:

views:

455

answers:

4

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

A: 

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

Varun
No. I need to expand a pop-up menu. In your link the example below, is not a popup menu to JTextField. But i am looking for this.
artaxerxe
A: 

Maybe editable combo box could suit you better.

Boris Pavlović
A: 

Read the JComponent API for the setComponentPopupMenu() method.

camickr
+2  A: 

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?

austen