Supreeto
Supreeto

Reputation: 308

Conversion of String to JComboBox

How do I convert string to JComboBox object model ?

Note: Im using NetBeans IDE 8.0.2

Upvotes: 0

Views: 1827

Answers (3)

user1908358
user1908358

Reputation: 13

I solved the problem.

I needed to be able to grab each line independent of the other so I can use just one Text file but break it out to different j combo boxes. Below was what I did. Is there a shorter way of doing this? I will have 20 JComboBox with each having around 7 select entries. The entries for the drop down boxes are around 50 lines of selections.

public void inputFile() throws IOException{

    //File reader method

    FileReader file = new FileReader("c:\\jcboEntries.dat");
    try (BufferedReader br = new BufferedReader(file)) {

         String[] lines = new String [6];
         String [] jcbo = new String [6];
         try {

            int i =0;
            lines[0] = br.readLine();
            jcbo[0] = lines[0];
            jcbo0 = jcbo[0];
            jcboNUMONE.addItem(jcbo0);
            System.out.println(jcbo0);

            lines[1] = br.readLine();
            jcbo[1] = lines[1];
            jcbo1 = jcbo[1];
            jcboNUMONE.addItem(jcbo1);
            System.out.println(jcbo1);

            lines[2] = br.readLine();
            jcbo[2] = lines[2];
            jcbo2 = jcbo[2];
            jcboNUMONE.addItem(jcbo2);
            System.out.println(jcbo2);

            lines[3] = br.readLine();
            jcbo[3] = lines[3];
            jcbo3 = jcbo[3];
            jcboNUMONE.addItem(jcbo3);
            System.out.println(jcbo3);

            lines[4] = br.readLine();
            jcbo[4] = lines[4];
            jcbo4 = jcbo[4];

            jcboNUMONE.addItem(jcbo4);
            System.out.println(jcbo4);

            lines[5] = br.readLine();
            jcbo[5] = lines[5];
            jcbo5 = jcbo[5];
            jcboNUMONE.addItem(jcbo5);
            System.out.println(jcbo5);


         } catch (IOException e){}

     } catch (IOException e){}    

}

Upvotes: 0

Mohammed Jamal
Mohammed Jamal

Reputation: 11

to change JComboBox from <String> to <Object>

  1. right click on JComboBox and select properties
  2. select code tab
  3. click on ... button from type parameters
  4. replace <String> to <Object> and click ok.

enter image description here

Upvotes: 1

camickr
camickr

Reputation: 324098

You don't convert a String to a ComboBoxModel. You add the String to the combo box or the ComboBoxModel.

For example:

JComboBox<String> comboBox = new JComboBox<String>();
comboBox.add( "One" );
comboBox.add( "Two" );
comboBox.add( "Three" );

Read the section from the Swing tutorial on How to Use Combo Boxes for more information and other examples.

You can also search the forum or web for other examples.

Upvotes: 0

Related Questions