Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

swing

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.

like image 302
Marquinio Avatar asked Dec 05 '25 00:12

Marquinio


2 Answers

You mean a JComboBox?

Reference:

  • How to use Combo Boxes
like image 71
Artems Avatar answered Dec 07 '25 15:12

Artems


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.

like image 33
ring bearer Avatar answered Dec 07 '25 14:12

ring bearer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!