Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: JComboBox help again but different help 75+ items in it

  1. #1
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default JComboBox help again but different help 75+ items in it

    so my program works like this you select an option from 1 combo box that determines what goes in a second combo box..this works nicely but each selection adds from 25-35 items each in to the second combo box for a total of around 100+ items

    each option from the second combo box sets a different number to an integer and each choice has a different number so my question is

    is there a better way to set the integer for each of these than to write 100+ if statements? i looked at the jcombobox api and didnt see anything that could help me...or maybe the only way is to write a bunch of if statements? any ideas on how i could do this without having a 2mb file of if statements? lol


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JComboBox help again but different help 75+ items in it

    You could put the value you want returned when a item is selected in the item itself.
    See the ClientProperty methods for JComponent. Its frontends a hash map where you can store any value you want in each component.

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: JComboBox help again but different help 75+ items in it

    I can't tell whether Norm is referring to what I've done below or not. Was your comment one suggestion or two, Norm?

    I remember from a recent thread that JComboBox takes an array of Objects in one of its constructors, so the visible items must be the result of .toString() on the original objects. You can create a convenience class to offer your JComboBox items' displayable content via the .toString() method, and bundle whatever other values you like as member data in each object:
    package com.javaprogrammingforums.domyhomework;
     
    import java.awt.event.*;
    import java.math.BigDecimal;
    import javax.swing.*;
     
    public class JComboObject extends JFrame
    {
      public static final long serialVersionUID = 42;
      public JComboObject()
      {
        ComboItem[] options = new ComboItem[]
         {
           new ComboItem("One", new BigDecimal(1))
          , new ComboItem("Two", new BigDecimal(2))
          , new ComboItem("Three", new BigDecimal(3))
         };
        setSize(200, 200);
        JComboBox jcb = new JComboBox(options);
        add(jcb);
        jcb.addActionListener(new ActionListener()
         {
           public void actionPerformed(ActionEvent event)
          {
            JOptionPane.showMessageDialog(JComboObject.this, ((ComboItem)((JComboBox)event.getSource()).getSelectedItem()).getValue().toString());
          }
         });
      }
      public static void main(String[] args)
      { new JComboObject().setVisible(true); }
    }
    class ComboItem
    {
      private final String key;
      private final BigDecimal value;
      public ComboItem(String k, BigDecimal v)
      { key = k; value = v; }
      /* toString() is what is displayed in the combobox dropdown */
      public String toString()
      { return key.toString(); }
      public BigDecimal getValue()
      {
        return value;
      }
    }
    Apologies for the actionPerformed - it saved some space but looks horrible. There are two casts and an outer-class-this in there.
    Last edited by Sean4u; August 27th, 2011 at 03:17 PM. Reason: typo

  4. The Following User Says Thank You to Sean4u For This Useful Post:

    derekxec (August 27th, 2011)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JComboBox help again but different help 75+ items in it

    Your solution has no overlap with mine. See the JComponent class's ClientProperty methods.
    For example:
    final String TheKey = "A KEY";
    JComboBox jcb1 = new JComboBox(...);
    jcb1.setClientProperty(TheKey, theValue);
    ...
    then get the reference to the source of the event into a JComponent object and
    TheValueType theValue = (TheValueType)src.getClientProperty(TheKey);

  6. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: JComboBox help again but different help 75+ items in it

    Ah ok ... it was the first bit which threw me. I thought after I'd tested out the JComboBox(Object[]) idea, that it was exactly what you were suggesting by
    in the item itself.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JComboBox help again but different help 75+ items in it

    Your class provides get/set methods for storing values for each item in the list.

  8. #7
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: JComboBox help again but different help 75+ items in it

    i see how that could work for me but im having trouble understanding how to use it...i read the clientproperty methods in the jcomponent api but i cant get my head around it

    what is the key? the value i think i understand but the key is throwing me off

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: JComboBox help again but different help 75+ items in it

    The client property uses something like a hashmap. Look at the API doc for that class.
    The key is what you give to a hashmap for it to return the value.

Similar Threads

  1. [SOLVED] Need help with displaying items with my program
    By mike101290 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2011, 08:07 AM
  2. Getting items from a table/checkboxes
    By beth in forum AWT / Java Swing
    Replies: 2
    Last Post: January 5th, 2011, 01:29 PM
  3. misalignment of list items
    By venkyInd in forum AWT / Java Swing
    Replies: 2
    Last Post: March 15th, 2010, 08:33 AM
  4. How to get a sum value from items listed in array ?
    By makarov in forum Loops & Control Statements
    Replies: 0
    Last Post: January 6th, 2010, 06:11 PM
  5. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 06:07 AM