tags:

views:

53

answers:

3

Hi everyone,

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.

+2  A: 

You mean a JComboBox?

Reference:

OMG Ponies
+2  A: 

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.

ring bearer
Yeah I think this will work. I'll give it a try. Thanks.
Marquinio
A: 

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.

Elvith