Search code examples
javaswing

Does Swing have a similar component like HTML <select> tag?


I want to display some information in a JMenuItem. The data format is like this:

1-1|Menu Name1

1-2|Menu Name2

My problem is that when I create the JMenuItem it displays the whole key-value pair. I'm wondering if there is a similar behavior in Swing like the HTML's

<SELECT>
 <OPTION value="1-1">Menu Name1</OPTION>
 <OPTION value="1-2">Menu Name2</OPTION>
</SELECT>

...where the user never sees the key, only sees the values.

I'm not familiar with Swing so if anyone knows a similar behavior like HTML's using JMenuItems style please share.

thanks in advance.


Solution

  • If I understand correctly, you need to set a different accessible property for a JMenuItem so that when the user clicks on the menu item, you have a corresponding value to read that can be used for some other purposes.

    For this, try setActionCommand API; This way you can associate any string with a Menu Item, and in event processing code, you can do a getActionCommand on the source object that generated the event.

    Note: you have 0% acceptance rate. That means you do not accept any answers for the question you posted. Don't expect too many people to answer your questions.