如何从文本文件中填充Java中的两个组合框?

如何从文本文件中填充Java中的两个组合框?,java,swing,combobox,jcombobox,Java,Swing,Combobox,Jcombobox,我试图从一个数据填充两个组合框,但不知道如何在两个组合框之间划分数据。数据现在填充在两个组合框中 这是我的数据文本文件: [Gates] value1 value2 value3 [Mids] customer1 customer2 下面是我在Java Swing Gui应用程序中的代码: private void populateCombos() throws FileNotFoundException { JFileChooser fileChooser = new JFile

我试图从一个数据填充两个组合框,但不知道如何在两个组合框之间划分数据。数据现在填充在两个组合框中

这是我的数据文本文件:

[Gates]
value1
value2
value3

[Mids]
customer1
customer2
下面是我在Java Swing Gui应用程序中的代码:

private void populateCombos() throws FileNotFoundException {

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    int result = fileChooser.showOpenDialog(frmGottApplication);

    BufferedReader input=new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
    if (result == JFileChooser.APPROVE_OPTION) {
        selectedFile = fileChooser.getSelectedFile();
        textFieldLoadConfig.setText(selectedFile.getAbsolutePath());
        lblConfRes.setText("Loaded " + selectedFile.getAbsolutePath().toString());
    } else {
        lblConfRes.setText("You didn't load...");
    }
    List<String> strings = new ArrayList<String>();
    try {
        String line = null;
        try {
            while ((line = input.readLine()) != null) {
                strings.add(line);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    } finally {
        try {
            input.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    String[] lineArrayGates = strings.toArray(new String[] {});

    comboBoxGate.removeAllItems();
    comboBoxMid.removeAllItems();

    for (String str : lineArrayGates) {
        comboBoxGate.addItem(str);
        comboBoxMid.addItem(str);
    }
}
private void populateCombos()引发FileNotFoundException{
JFileChooser fileChooser=新的JFileChooser();
fileChooser.setCurrentDirectory(新文件(System.getProperty(“user.home”));
fileChooser.setFileSelectionMode(仅限JFileChooser.FILES_);
int result=fileChooser.showOpenDialog(frmGottApplication);
BufferedReader输入=新的BufferedReader(新的文件读取器(fileChooser.getSelectedFile());
if(result==JFileChooser.APPROVE\u选项){
selectedFile=fileChooser.getSelectedFile();
textFieldLoadConfig.setText(selectedFile.getAbsolutePath());
lblConfRes.setText(“已加载”+selectedFile.getAbsolutePath().toString());
}否则{
setText(“您没有加载…”);
}
列表字符串=新的ArrayList();
试一试{
字符串行=null;
试一试{
而((line=input.readLine())!=null){
字符串。添加(行);
}
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}最后{
试一试{
input.close();
}捕获(IOE1异常){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
String[]lineArrayGates=strings.toArray(新字符串[]{});
comboBoxGate.removeAllItems();
comboBoxMid.removeAllItems();
用于(字符串str:lineArrayGates){
comboBoxGate.addItem(str);
ComboxMid.addItem(str);
}
}
从代码中可以看出,我正在从外部文本文件读取数据,然后尝试将其加载到两个不同的组合框中。但是如何编写代码,将gate的值划分为第一个组合,将mid的值划分为第二个组合。 有什么建议吗?
谢谢

问题从文件及其内容开始,用更合适的格式定义属性文件…您可以使用json、xml、yaml或仅使用属性

我将用老式的java属性来做这个例子

文件:

门=值1、值2、值3

Mids=客户1,客户2

然后将其作为属性读取,将其拆分为StringArray并用其填充框

public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;

try {
    input = new FileInputStream("./file2.txt");
    prop.load(input);
    String[] gates = prop.getProperty("Gates").split(",");
    String[] mids = prop.getProperty("Mids").split(",");
    JFrame myJFrame = new JFrame();
    myJFrame.setTitle("Example");
    myJFrame.setSize(250, 250);
    JPanel panel = new JPanel();

    JComboBox<String> myComboGates = new JComboBox<>(gates);
    JComboBox<String> myComboMids = new JComboBox<>(mids);
    panel.add(myComboGates);
    panel.add(myComboMids);

    myJFrame.add(panel);
    myJFrame.setVisible(true);

} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
    try {
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
}

}
publicstaticvoidmain(字符串[]args){
Properties prop=新属性();
InputStream输入=null;
试一试{
输入=新文件输入流(“./file2.txt”);
道具载荷(输入);
String[]gates=prop.getProperty(“gates”).split(“,”);
字符串[]mids=prop.getProperty(“mids”).split(“,”);
JFrame myJFrame=新JFrame();
myJFrame.setTitle(“示例”);
myJFrame.setSize(250250);
JPanel面板=新的JPanel();
JComboBox myComboGates=新的JComboBox(门);
JComboBox myComboMids=新JComboBox(mids);
面板。添加(门);
面板。添加(myComboMids);
添加(面板);
myJFrame.setVisible(true);
}捕获(IOEX异常){
例如printStackTrace();
}最后{
如果(输入!=null){
试一试{
input.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
}
结果是一个道具上有两个组合和两种不同的信息。文件: