简体   繁体   English

从文件读取数据到JCombobox

[英]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. 我正在制作一个GUI,其中有6个组合框,我从文本文件读取数据到这些组合框。 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 我的文本文件具有3行和2列,因此当我读取数据时,只有前2个组合框填充有文本文件第3行而不是第1行的值的数据,其余的组合框仍然保留我的文本文件包含6个值,应该在6个组合框中显示。

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

Please look at your code, you are filling data only to combobox 1 and 2, other comboboxes you are not filling any data. 请查看您的代码,您仅将数据填充到组合框1和2,其他组合框则未填充任何数据。

If you want data in other compoboxes, fill them as well in the while loop. 如果要在其他组合框中添加数据,请同时在while循环中填充它们。

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
}

Declare a JCombobox array and store all your JCombobox in the array as below, 声明一个JCombobox数组,并将您所有的JCombobox存储在该数组中,如下所示,

    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, 声明一个Arraylist并存储您从文件中读取的数据,如下所示,

// 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. 最后,遍历每个Arraylist并填充组合框。

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

This should do good. 这应该很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM