user
user

Reputation: 27

Reading data from file to JCombobox

I am making a GUI in which i have 6 combo boxes, i read data from a text file to these combo boxes. My text file has 3 rows and 2 columns, so when i read data only my first 2 combo boxes is getting populated with data that to with the values of the 3rd row of the text file instead of the 1st row and the remaining combo boxes remains empty.As my text file contains 6 values it should display in the 6 combo boxes.Please help

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class Read extends JPanel {
    public Read() {
        JPanel buttonPanel = new JPanel();
        add(buttonPanel);
        buttonPanel.setLayout(new GridLayout(0, 2, 5, 5));

        JComboBox comboBox1 = new JComboBox();
        comboBox1.addItem("1");
        comboBox1.addItem("2");
        comboBox1.addItem("4");

        JComboBox comboBox2 = new JComboBox();
        comboBox2.addItem("1");
        comboBox2.addItem("2");
        comboBox2.addItem("4");

        JComboBox comboBox3 = new JComboBox();
        comboBox3.addItem("1");
        comboBox3.addItem("2");
        comboBox3.addItem("4");

        JComboBox comboBox4 = new JComboBox();
        comboBox4.addItem("1");
        comboBox4.addItem("2");
        comboBox4.addItem("4");

        JComboBox comboBox5 = new JComboBox();
        comboBox5.addItem("1");
        comboBox5.addItem("2");
        comboBox5.addItem("4");

        JComboBox comboBox6 = new JComboBox();
        comboBox6.addItem("1");
        comboBox6.addItem("2");
        comboBox6.addItem("4");

        buttonPanel.add(comboBox1);
        buttonPanel.add(comboBox2);
        buttonPanel.add(comboBox3);
        buttonPanel.add(comboBox4);
        buttonPanel.add(comboBox5);
        buttonPanel.add(comboBox6);

        try{
            InputStream ips=new FileInputStream("tl.txt"); 
            InputStreamReader ipsr=new InputStreamReader(ips);
            BufferedReader br=new BufferedReader(ipsr);
            String line;
            while ((line=br.readLine())!=null) {
                String[] s = line.split(" ");
                comboBox1.setSelectedItem(s[0]);
                comboBox2.setSelectedItem(s[1]);
            }
            br.close(); 
        }       
        catch (Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Read a = new Read();
        JFrame f = new JFrame("");
        f.getContentPane().add(a);
        f.setSize(300,200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
}

text file

2 4
1 2
4 1

Upvotes: 0

Views: 4174

Answers (2)

Ironluca
Ironluca

Reputation: 3762

Please look at your code, you are filling data only to combobox 1 and 2, other comboboxes you are not filling any data.

If you want data in other compoboxes, fill them as well in the while loop.

Hope this helps.

thanks


public class Read extends JPanel{

   String[] values=new String[6];
   JCombobox<String>[] combos=new JCombobox<String>[6];

   public Read() {

    //Do your layout initialization operations here

    this.initCombo();

    //Put the logic to add the comboboxes to the UI here

   }

   public void intiCombo(){
    try{
                ArrayList<String> tmp=new ArrayList<String>();
                InputStream ips=new FileInputStream("tl.txt"); 
                InputStreamReader ipsr=new InputStreamReader(ips);
                BufferedReader br=new BufferedReader(ipsr);
                String line;
                while ((line=br.readLine())!=null) {
                    String[] s = line.split(" ");
                    tmp.add(s[0]);
                    tmp.add(s[1]);
                }
                br.close(); 
            }       
            catch (Exception e){
                e.printStackTrace();
            }

         values=tmp.toArray(new String[1]);
         for(int i=0;i<conbos.length;i++)combos[i]=new JCombobox(values);

   }


    //Your main method comes here
}

Upvotes: 1

Amarnath
Amarnath

Reputation: 8865

Declare a JCombobox array and store all your JCombobox in the array as below,

    JComboBox[] comboBoxs = new JComboBox[6];
    comboBoxs[0] = comboBox1;
    comboBoxs[1] = comboBox2;
    comboBoxs[2] = comboBox3;
    comboBoxs[3] = comboBox4;
    comboBoxs[4] = comboBox5;
    comboBoxs[5] = comboBox6;

Declare a Arraylist and store the data which you read from the file as below,

// Other code goes here.
ArrayList<String> list = new ArrayList<String>();
String line;
    while ((line=br.readLine())!=null) {
        String[] s = line.split(" ");
        list.add(s[0]);
        list.add(s[1]);
    }
br.close();

Finally loop over each Arraylist and populate the Combobox's.

for(int i = 0; i < comboBoxs.length; i++) {
        comboBoxs[i].setSelectedItem(list.get(i));
    }

This should do good.

Upvotes: 2

Related Questions