0

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)

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by javaAddict
0

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

0

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

0

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
  }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.