Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JCombobox assistance required.

Status
Not open for further replies.

JProg

Programmer
Apr 4, 2002
88
0
0
JP
Dear Everyone,

At the moment I am working on a UI that utilises four JComboboxes. Each of the four JComboboxes contains an index of 10 numeric values, namely 0 - 9. I intend on using the concatenated values of all four boxes to represent a four digit year value, e.g.: 1583, 1902, 2050, 9504, etc.

I need to write some code that listens to each of the four JComboboxes and places the user selected number (from the JComboboxes index) into a variable.

Once the values of all four JComboboxes are held in four variables I then need to concatenate the variables so that they represent a single year, e.g.: 1583.

Once I have the concatenated value I will then perform relative mathematical operations.

If anybody has any suggestions as to how I can listen to the JComboboxes and then place the selected index values into variables they will be greatly appreciated.

Thanks
 
Okay below is a class that should compile any any JDK 1.2 or higher. It shows how to handle adding data to a JComboBox, and obtaining this data when a selection is made. (You should be able to extrapulate this to multiple comboboxes) It runs in two modes, default uses Integer objects in the combo box, the other uses String objects in the combo box. You just pass a command line arg of -useStrings to do just that. This is to show just some of the flexiblity of a JComboBox. The I assummed that if you are going to do some calculations on this selected object that you would want to use Integers objects, but you mentioned doing some concatenations prior which you may want String objects. They are both there for you.

I am sorry, but I do not know why you would want to concatenate the selected item, or what you would concatenate it to. I am assuming you do know know that you are returned the actuly object that was selected. So if the String object is "2002" then you would get this object that represents 2002. So if this is the value you want to work with then your set.

If you have any further questions please don't hesitate to ask.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxExample extends JFrame
{
  private String      selectedString  = null;
  private Integer     selectedInteger = null;
  private JComboBox   comboBox        = null;
  private boolean     useStrings      = false;

  public ComboBoxExample(boolean useStrings)
  {
    this.useStrings = useStrings;

    String[] stringYears =
    {
      "2002", "2001", "2000", "1999", "1998",
      "1997", "1996", "1995", "1994", "1993"
    };

    Integer[] integerYears =
    {
      new Integer(2002), new Integer(2001),
      new Integer(2000), new Integer(1999),
      new Integer(1998), new Integer(1997),
      new Integer(1996), new Integer(1995),
      new Integer(1994), new Integer(1993)
    };

    if (useStrings)
      comboBox = new JComboBox(stringYears);
    else
      comboBox = new JComboBox(integerYears);

    handleComboBoxSelection();

    comboBox.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        handleComboBoxSelection();
      }
    });

    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(comboBox);
    setSize(100, 65);
  }

  private void handleComboBoxSelection()
  {
    if (useStrings)
    {
      selectedString = (String)comboBox.getSelectedItem();

      System.err.println("selectedYear(String): " +
                          selectedString);
    }
    else
    {
      selectedInteger = (Integer)comboBox.getSelectedItem();

      System.err.println("selectedYear(Integer): " +
                          selectedInteger);
    }
  }

  public static void main(String[] args)
  {
    boolean useStings = false;

    if (args != null    &&
        0 < args.length &&
        args[0].equalsIgnoreCase(&quot;-useStrings&quot;))
      useStings = true;

    ComboBoxExample example = new ComboBoxExample(useStings);
    example.show();
  }
}
Rodney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top