Results 1 to 5 of 5
  1. #1
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default 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]);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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:
    1. 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.
    2. 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.

  3. #3
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: JComboBox change text but keep the values

    Can you give me an example on how to use that? I have seen that before :(

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Xzar is offline Member
    Join Date
    Nov 2012
    Location
    Victoria, BC
    Posts
    14
    Rep Power
    0

    Default Re: JComboBox change text but keep the values

    alright thanks!

Similar Threads

  1. Replies: 1
    Last Post: 09-02-2012, 05:27 AM
  2. Showing different values in JComboBox inside JTable
    By d3n1s in forum Advanced Java
    Replies: 2
    Last Post: 05-02-2011, 08:25 PM
  3. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  4. Get value of JComboBox text field
    By nik_meback in forum AWT / Swing
    Replies: 0
    Last Post: 01-07-2011, 02:48 PM
  5. Need help with highlighting text in a jcombobox
    By cjmartin in forum AWT / Swing
    Replies: 3
    Last Post: 08-09-2010, 06:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •