Java Swing应用程序尝试在单击按钮时填充JComboBox

Java Swing应用程序尝试在单击按钮时填充JComboBox,java,swing,Java,Swing,我正在学习Java Swing。我正在尝试开发一个简单的应用程序,用于学习目的。以下代码中存在多个问题。我尝试读取csv文件并在单击按钮时填充JComboBox public class MyForm { private JButton btnRead; private JButton btnRead2; private JComboBox cbCodes; private JPanel mainPanel; private DefaultComb

我正在学习Java Swing。我正在尝试开发一个简单的应用程序,用于学习目的。以下代码中存在多个问题。我尝试读取csv文件并在单击按钮时填充JComboBox

    public class MyForm {
    private JButton btnRead;
    private JButton btnRead2;
    private JComboBox cbCodes;
    private JPanel mainPanel;
    private DefaultComboBoxModel comboBoxModel;

    public MyForm(){
        // issue 1: I always get null pointer exception in this line  
        comboBoxModel = new DefaultComboBoxModel();
        cbCodes = new JComboBox(comboBoxModel);

        btnRead.addActionListener( e -> {
             List<String[]> data =   readData();
             comboBoxModel.removeAllElements();
             data.forEach(item -> comboBoxModel.addElement(item));
        });

      // issue 2: Since DefaultComboBoxModel was not working. I tried without it. As this I get correct data in the array. But when I make JComboBox with array. Nothing is filled. It is empty. 
      btnRead2.addActionListener( e -> {
            List<String[]> data =   readData();
            String[] array = new String[data.size()];
            data.toArray(array); 
            cbCodes = new JComboBox(array);
        });
    }

   // issue 3: I can't complie the code without this empty method. Why do I need it? 
   // error: Form contains components with Custom Create option but no createUIComponents() method
    void createUIComponents(){
       
    }

    public List<String[]> readData() {
        String file = "data.csv";
        List<String[]> content = new ArrayList<>();
        try(BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line = "";
            while ((line = br.readLine()) != null) {
                if(line.contains("\"")){
                    content.add(line.split(" "));
                }
                content.add(line.split(","));
            }
        } catch (FileNotFoundException e) {
            //Some error logging
        } catch (IOException e) {
            e.printStackTrace();
        }
        return content;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MyForm");
        frame.setContentPane(new MyForm().mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
公共类MyForm{
私人JButton btnRead;
私人JButton btnRead2;
专用JComboBox CBCODE;
私人JPanel主面板;
私有DefaultComboxModel ComboxModel;
公共MyForm(){
//问题1:我总是在这一行中遇到空指针异常
comboBoxModel=新的DefaultComboxModel();
cbCodes=新的JComboxBox(ComboxModel);
btnRead.addActionListener(e->{
List data=readData();
comboBoxModel.removeAllElements();
data.forEach(项目->comboBoxModel.addElement(项目));
});
//问题2:因为DefaultComboxModel无法工作。我尝试不使用它。因此我在数组中获得了正确的数据。但是当我使用数组创建JComboxModel时,没有填充任何内容。它是空的。
btnRead2.addActionListener(e->{
List data=readData();
String[]数组=新字符串[data.size()];
数据。toArray(数组);
cbCodes=新的JComboBox(阵列);
});
}
//问题3:没有这个空方法,我无法编译代码。为什么我需要它?
//错误:表单包含具有自定义创建选项但没有createUIComponents()方法的组件
void createUIComponents(){
}
公共列表读取数据(){
String file=“data.csv”;
列表内容=新建ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(file))){
字符串行=”;
而((line=br.readLine())!=null){
如果(第行包含(\“”)){
content.add(行分割(“”);
}
content.add(行分割(“,”);
}
}catch(filenotfounde异常){
//一些错误记录
}捕获(IOE异常){
e、 printStackTrace();
}
返回内容;
}
公共静态void main(字符串[]args){
JFrame=新JFrame(“MyForm”);
frame.setContentPane(新的MyForm().mainPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
我在源代码中提出了我的问题,并在这里给出了注释,以准确地显示这些问题所涉及的内容

  • 您在指定的行中没有得到NullPointerException,而是在
    btnRead.addActionListener(e->{
    行中,因为btnRead尚未初始化

  • 创建新的JComboBox时,也必须将其添加到面板中。仅使用new创建它不会显示它。但它背后的真正问题是:您使用的模型错误。请编写

        comboBoxModel.removeAllElements();
        for (final String string : array) {
            comboBoxModel.addElement(string);
        }
    
  • 解决这个问题

  • 这里的问题不在您提供的代码中,而是来自另一个组件。在某些情况下,有人使用了UI设计器。这些设计器通常创建初始化方法,就像createUIComponents一样。请查看该方法的调用位置
  • 简介:

    • 总而言之,你的代码真的很混乱。从新代码重新构造,这将解决很多问题
      • 并尽快初始化UI组件,最好在声明行中执行:
        private final JButton btnRead=new JButton(“Read!”);
    • 我强烈建议使用像Eclipse或IntelliJ这样的IDE,这样可以帮助您编写干净的代码,更容易地查看和纠正问题
  • 您在指定的行中没有得到NullPointerException,而是在
    btnRead.addActionListener(e->{
    行中,因为btnRead尚未初始化

  • 创建新的JComboBox时,也必须将其添加到面板中。仅使用new创建它不会显示它。但它背后的真正问题是:您使用的模型错误。请编写

        comboBoxModel.removeAllElements();
        for (final String string : array) {
            comboBoxModel.addElement(string);
        }
    
  • 解决这个问题

  • 这里的问题不在您提供的代码中,而是来自另一个组件。在某些情况下,有人使用了UI设计器。这些设计器通常创建初始化方法,就像createUIComponents一样。请查看该方法的调用位置
  • 简介:

    • 总而言之,你的代码真的很混乱。从新代码重新构造,这将解决很多问题
      • 并尽快初始化UI组件,最好在声明行中执行:
        private final JButton btnRead=new JButton(“Read!”);
    • 我强烈建议使用像Eclipse或IntelliJ这样的IDE,这样可以帮助您编写干净的代码,更容易地查看和纠正问题

    我使用IntellJ编写此代码。我使用GUI工具。在学习Swing时,我看到了一些教程和YouTube视频。当我通过GUI编辑器工具将组件添加到主JPanel时,我得到了一个假设,所有组件都已初始化并添加到面板中。我不需要像这样通过代码添加它。mainPanel.add(cbCodes)。我将这一行添加到构造函数中。在单击按钮并读取csv文件后,它仍然不会填充。它只是空的。是的。在我对(2)的回答中,我向您展示了一些代码。将此代码插入到您的
    readData()中
    method,因此数据模型获取数据。在您的方法中,您不必创建列表然后添加,还可以在方法的开头使用
    comboBoxModel.removeAllElements();
    读取每一行时,只需执行
    ComboxModel.addElement(行)
    我使用IntellJ编写此代码。我使用GUI工具。在学习Swing时,我看到了一些教程和YouTube视频。当我通过GUI编辑器工具将组件添加到主JPanel时,我得到了一个假设,所有组件都已初始化并添加到面板中。我不需要像这样通过代码添加它。mainPanel.add(cbCodes).我在构造函数中添加了这一行。仍然在