Java Swing应用程序尝试在单击按钮时填充JComboBox
我正在学习Java Swing。我正在尝试开发一个简单的应用程序,用于学习目的。以下代码中存在多个问题。我尝试读取csv文件并在单击按钮时填充JComboBoxJava 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
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);
}
}
我在源代码中提出了我的问题,并在这里给出了注释,以准确地显示这些问题所涉及的内容
btnRead.addActionListener(e->{
行中,因为btnRead尚未初始化
comboBoxModel.removeAllElements();
for (final String string : array) {
comboBoxModel.addElement(string);
}
- 总而言之,你的代码真的很混乱。从新代码重新构造,这将解决很多问题
-
- 并尽快初始化UI组件,最好在声明行中执行:
private final JButton btnRead=new JButton(“Read!”);
- 并尽快初始化UI组件,最好在声明行中执行:
- 我强烈建议使用像Eclipse或IntelliJ这样的IDE,这样可以帮助您编写干净的代码,更容易地查看和纠正问题
btnRead.addActionListener(e->{
行中,因为btnRead尚未初始化
comboBoxModel.removeAllElements();
for (final String string : array) {
comboBoxModel.addElement(string);
}
- 总而言之,你的代码真的很混乱。从新代码重新构造,这将解决很多问题
-
- 并尽快初始化UI组件,最好在声明行中执行:
private final JButton btnRead=new JButton(“Read!”);
- 并尽快初始化UI组件,最好在声明行中执行:
- 我强烈建议使用像Eclipse或IntelliJ这样的IDE,这样可以帮助您编写干净的代码,更容易地查看和纠正问题
readData()中
method,因此数据模型获取数据。在您的方法中,您不必创建列表然后添加,还可以在方法的开头使用comboBoxModel.removeAllElements();
读取每一行时,只需执行ComboxModel.addElement(行)
我使用IntellJ编写此代码。我使用GUI工具。在学习Swing时,我看到了一些教程和YouTube视频。当我通过GUI编辑器工具将组件添加到主JPanel时,我得到了一个假设,所有组件都已初始化并添加到面板中。我不需要像这样通过代码添加它。mainPanel.add(cbCodes).我在构造函数中添加了这一行。仍然在