简体   繁体   English

无法从文件读取数据到JComboBox

[英]Cannot read data from file to JComboBox

I am trying to make a ui in which i read file from text file to JComboBoxes and display the results in JTextFields.Only the first combobox is getting updated from the file while the rest is not getting updated from the file. 我正在尝试制作一个ui,其中我将文本文件中的文件读取到JComboBoxes并在JTextFields中显示结果。只有第一个组合框正在从文件中更新,而其余的组合框都没有从文件中更新。

Here is my Code: 这是我的代码:

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

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


JTextField field1 = new JTextField(5);
field1.setEditable(false);
buttonPanel.add(field1);
JTextField field2 = new JTextField(5);
field2.setEditable(false);
buttonPanel.add(field2);
JTextField field3 = new JTextField(5);
field3.setEditable(false);
buttonPanel.add(field3);
JTextField field4 = new JTextField(5);
field4.setEditable(false);
buttonPanel.add(field4);

JComboBox comboBox = new JComboBox();
comboBox.setEditable(true);

 comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            JComboBox comboBox = (JComboBox) event.getSource();

            field1.setText((String) comboBox.getSelectedItem());

        }
    });

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


 comboBox1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            JComboBox comboBox1 = (JComboBox) event.getSource();

            Object selected = comboBox1.getSelectedItem();
            if(selected.toString().equals("1"))
            field2.setText("3");
            else if(selected.toString().equals("2"))
                field2.setText("6");
    else if(selected.toString().equals("4"))
        field2.setText("12");


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

 comboBox2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            JComboBox comboBox2 = (JComboBox) event.getSource();

            Object selected = comboBox2.getSelectedItem();
            if(selected.toString().equals("1"))
            field3.setText("1");
            else if(selected.toString().equals("2"))
                field3.setText("2");
    else if(selected.toString().equals("4"))
        field3.setText("4");


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

 comboBox3.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {

            JComboBox comboBox3 = (JComboBox) event.getSource();

            Object selected = comboBox3.getSelectedItem();
            if(selected.toString().equals("1"))
            field4.setText("1");
            else if(selected.toString().equals("2"))
                field4.setText("2");
        }
    });

buttonPanel.add(comboBox);
buttonPanel.add(comboBox1);
buttonPanel.add(comboBox2);
buttonPanel.add(comboBox3);

try{
        InputStream ips=new FileInputStream("test.txt"); 
        InputStreamReader ipsr=new InputStreamReader(ips);
        BufferedReader br=new BufferedReader(ipsr);
        String line;
        while ((line=br.readLine())!=null) {
    String[] s = line.split(" ");
    comboBox.setSelectedItem(s[0]);
    comboBox1.setSelectedItem(s[1]);
    comboBox2.setSelectedItem(s[2]);
    comboBox3.setSelectedItem(s[3]);
        }
        br.close(); 
    }       
    catch (Exception e){
        e.printStackTrace();
    }


  }
  public static void main(String[] args) {
 Main1 a = new Main1();
 JFrame f = new JFrame();
 f.getContentPane().add(a);
 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 f.pack();
 f.setVisible(true);
  }
 }

Text File: 文本文件:

14 54 89 56

None of the values in file list are contained within the combo box list so none appear except for the first one. 文件列表中的值均未包含在组合框列表中,因此除第一个以外均未出现。 This happens because this is the only one that is editable. 发生这种情况是因为这是唯一可编辑的。 From the docs 来自文档

If anObject is not in the list and the combo box is uneditable, it will not change the current selection 如果anObject不在列表中,并且组合框不可编辑,则不会更改当前选择

Simply add all expected items to the combo boxes models on application startup. 只需在应用程序启动时将所有期望的项目添加到组合框模型即可。

is this cos u have given " " instead of "\\n" in the stream reader ? 您是否在流阅读器中将此cos指定为“”而不是“ \\ n”? i mean in the Split function 我的意思是在拆分功能

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

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