Search code examples
javaswingjcomboboxjtextfield

How to retrieve data from JComboBox and store it in JTextFields?


I have a JComboBox. I want to retrieve the data from it. I know I use the getSelectedItem() method. But then once I get it, I want it to convert it to a String somehow..or ultimately, display it on a JTextField.

 String[] fluids = { "      ", "string 1", "String 2"}; 
 fluidsList = new JComboBox(fluids);

Solution

  • Assuming jTextField1 is your JTextField and fluidsList is your JComboBox, you'll have to have something like this

    jTextField1.setText(fluidsList.getSelectedItem() == null ? "" : fluidsList.getSelectedItem().toString() );