Results 1 to 5 of 5
- 11-14-2012, 06:00 AM #1
Member
- Join Date
- Nov 2012
- Location
- Victoria, BC
- Posts
- 14
- Rep Power
- 0
JComboBox change text but keep the values
Can i change the text in a JComboBox but keep what the values are?
I have a combo box that holds all the available DisplayModes of the monitor, and the user can switch in between them. However the combobox isn't very readable. An example of one of the selections is "java.awt.DisplayMode@84c" which doesn't mean anything to me, or the user. That particular DisplayMode means 640x480 60Hz 32 bit rate.
How can I change the text but keep it so I can change DisplayModes using this drop down menu?
The way the DisplayModes are added to the list is:
Java Code:for (int i = 0; i < DisplayModes.length; i++) { if (DisplayModes[i].getWidth() <= defaultSize.width && DisplayModes[i].getHeight() <= defaultSize.height) { cBox.addItem(DisplayModes[i]); } }
-
Re: JComboBox change text but keep the values
Your JComboBox is doing what it is built to do: display the default String returned by a call to toString() on the object it is displaying. The default method held by the Object class returns a String showing the item's class, the @ symbol and its hashCode value displayed in hex form.
To display more informative Strings, you have one of two options:
- The quick and dirty way is to give the DisplayMode class a decent toString() method override that returns a String that would display well in the JComboBox.
- The better way is to give the ListCellRenderer<DisplayMode> that helps show a String based on the object it represents that makes sense. This can most easily be done using a DefaultListCellRenderer.
- 11-14-2012, 06:29 AM #3
Member
- Join Date
- Nov 2012
- Location
- Victoria, BC
- Posts
- 14
- Rep Power
- 0
Re: JComboBox change text but keep the values
Can you give me an example on how to use that? I have seen that before :(
- 11-14-2012, 05:14 PM #4
Re: JComboBox change text but keep the values
Read the API for JComboBox and follow the link to the tutorial. It's all explained there.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-14-2012, 11:38 PM #5
Member
- Join Date
- Nov 2012
- Location
- Victoria, BC
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
JcomboBox master, change the values of all jComboBoxes on a Jatable
By masaMuscular in forum AWT / SwingReplies: 1Last Post: 09-02-2012, 05:27 AM -
Showing different values in JComboBox inside JTable
By d3n1s in forum Advanced JavaReplies: 2Last Post: 05-02-2011, 08:25 PM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
Get value of JComboBox text field
By nik_meback in forum AWT / SwingReplies: 0Last Post: 01-07-2011, 02:48 PM -
Need help with highlighting text in a jcombobox
By cjmartin in forum AWT / SwingReplies: 3Last Post: 08-09-2010, 06:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks