Hi and thanks for viewing my thread,
i have a problem with JComboBox.
in my situation, i have a code that display JTextArea and JComboBox.
this is my code:

public class JScrollPanes extends JApplet
{
  //--------------------------------------------------
 
    static String[] t_value2 = { "2", "3", "4", "5", "6" };
    
   // -----------------------------------------------

  private JButton generate_btn = new JButton("GO ");

  private Label label = new Label ("Enter Parameter Values");
  private Label labelt = new Label ("Choose t Value");

  private JTextArea
  t1 = new JTextArea("", 1, 20),
  t3 = new JTextArea("", 6, 20),
  t4 = new JTextArea("",13, 20);

  private JComboBox t2 = new JComboBox(t_value2);

class B4L implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
     {

        t3.setText("");
        t4.setText("");
      // get input from JTextArea
       String data_str=t1.getText();
     // get input from JComboBox...how??
     // count parameter
       int p = data_str.replaceAll("[^,]","").length();
       p++; // as an array size
       data = new int [p];
       int k =0;
       t3.append("Pairwise Test Set\n");
       t3.append("Parameters ="+Integer.toString(p)+"\n");
     }
  }

public void init()
  {
     //setting and initializing....
  }

  public static void main(String[] args)
  {
    run(new JScrollPanes(), 300, 500);
  }

  public static void run(JApplet applet, int width, int height)
  {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet);
    frame.setSize(width, height);
    applet.init();
    applet.start();
    frame.setVisible(true);
  }
}

i hope u guys understand my code....i cut the rest and juz show my problem...as u can see my combobox as t2...
my questions:
1) How to get index from dropdown menu. like JtextArea we use getText==> String data_str=t1.getText();
How we do in JComboBox. i want to get the data because i want to convert string to integer.
2) How to display the menu that user selected in t3(textarea 3)

Recommended Answers

All 3 Replies

comboBox.getSelectedItem().toString()
this code is used to get the selected item in combo box

comboBox.getSelectedItem().toString()
this code is used to get the selected item in combo box

yup. i got it!!

1) To get d item from dorpdown menu:
String tvalue_str=(String)t2.getSelectedItem();

2) this is d way i write to display the selected item :
int3.append("Value of T ="+tvalue_str+"\n");

thanks sennat_26

yup. i got it!!

1) To get d item from dorpdown menu:
String tvalue_str=(String)t2.getSelectedItem();

2) this is d way i write to display the selected item :
int3.append("Value of T ="+tvalue_str+"\n");

thanks sennat_26

I would like to add that in the same way you added String to the JComboBox, you can add objects:

class SomeObject {
  // many attributes
}
JComboBox t2 = new JComboBox();
// add some SomeObject objects to the JComboBox 


SomeObject so = (SomeObject)t2.getSelectedItem();

Also what will be displayed at the JComboBox will be determined by the toString method of SomeObject class. So don't forget to override it:

class SomeObject {
  // many attributes

  public Stirng toString() {
    return ""; // return whatever you want to be displayed at the options of ComboBox
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.