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 whol开发者_Go百科e 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.
You mean a JComboBox?
Reference:
- How to use Combo Boxes
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.
If you wrap your Key-Value Pair within a single object, you can pass those objects to your JMenu. To make sure, that only a part of the objects information (in this case the value) is displayed within this menu, you might either want to "hack" the rendering of the JMenuItem (by deriving your own class and messing with paintComponent(), I guess), or just change the toString()-method of the passed object (if practicable, this might lead to some problems depending on the further use of those objects) to return only the value, that should be displayed.
精彩评论