我还在玩Java和摇摆(还是很新的)。我试图用.txt文件中的数据填充JComboBox。我将数据拉入ArrayList并尝试使用ArrayList变量填充JComboBox。但是,当我运行应用程序时,组合框是空白的。
这是数组代码:
private ArrayList<String> list = new ArrayList<String>();
文件阅读器代码:
private void fileRead(){
try{
Scanner scan = new Scanner(new File("Examiner.txt"));
// ArrayList<String> list = new ArrayList<String>();
while(scan.hasNext()){
list.add(scan.next());
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
我用于组合框的混乱:
private void comboBoxes(){
panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = list.toArray(new String[list.size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTeachedCourses);
JComboBox comboBox2 = new JComboBox();
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
add(panel, BorderLayout.LINE_START);
}
这个类的全部代码。
package messing with swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.EmptyBorder;
public class ReportGUI extends JFrame{
//Fields
private JButton viewAllReports = new JButton("View All Program Details");
private JButton viewPrograms = new JButton("View Programs and Majors Associated with this course");
private JButton viewTeachedCourses = new JButton("View Courses this Examiner Teaches");
private JLabel courseLabel = new JLabel("Select a Course: ");
private JLabel examinerLabel = new JLabel("Select an Examiner: ");
private JPanel panel = new JPanel(new GridLayout(6,2,4,4));
private ArrayList<String> list = new ArrayList<String>();
//private String storeAllString="";
public ReportGUI(){
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}
private void fileRead(){
try{
Scanner scan = new Scanner(new File("Examiner.txt"));
// ArrayList<String> list = new ArrayList<String>();
while(scan.hasNext()){
list.add(scan.next());
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
private void reportInterface(){
setTitle("Choose Report Specifications");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel(new FlowLayout());
add(panel, BorderLayout.CENTER);
setSize(650,200);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
}
private void allReportsBtn(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(70, 50, 70, 25));
panel.add(viewAllReports);
viewAllReports.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JFrame AllDataGUI = new JFrame();
new AllDataGUI();
}
});
add(panel, BorderLayout.LINE_END);
}
private void comboBoxes(){
panel.setBorder(new EmptyBorder(0, 5, 5, 10));
String[] comboBox1Array = list.toArray(new String[list.size()]);
JComboBox comboBox1 = new JComboBox(comboBox1Array);
panel.add(examinerLabel);
panel.add(comboBox1);
panel.add(viewTeachedCourses);
JComboBox comboBox2 = new JComboBox();
panel.add(courseLabel);
panel.add(comboBox2);
panel.add(viewPrograms);
add(panel, BorderLayout.LINE_START);
}
}
我出错的任何想法?
答案 0 :(得分:3)
public ReportGUI()
{
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}
正如Obicere指出的那样:
comboBoxes()
,它会创建组合框并使用空列表填充组合框。fileRead()
方法将数据添加到List中,但这不会更新组合框的模型。代码顺序需要颠倒过来:
fileRead();
comboBoxes();
答案 1 :(得分:2)
塔卡仔细看看你正在做的事情......
public ReportGUI(){
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}
首先,您致电reportInterface
,初始化您的相框......
其次,你打电话给allReportsBtn
,这会创建你的按钮......
第三,您拨打comboBoxes
,将List
的内容应用到您的组合框中,该组合框为空...
第四,你调用fileRead
,它从文件中读取值......
您提供给JComboBox
的数组或您读取文件的List
值之间没有任何关系,因此即使您告诉组合框这些值已更改,也不会看到那个改变
尝试做更像
的事情public ReportGUI(){
reportInterface();
allReportsBtn();
fileRead();
comboBoxes();
}
,而不是...