[英]Split String read from text file, passed into ArrayList, then displayed in JComboBox - Java Swing
[英]JComboBox from ArrayList<String> - Not working - Java Swing
我仍在使用Java和swing(仍然很新)。 我正在尝试使用.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);
}
}
有什么想法我要去哪里吗?
public ReportGUI()
{
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}
正如Obicere指出的那样:
comboBoxes()
,它将创建组合框,并用一个空的List填充组合框。 fileRead()
方法,该方法会将数据添加到列表中,但这不会更新组合框的模型。 代码顺序需要颠倒:
fileRead();
comboBoxes();
塔卡仔细看看你做的顺序...
public ReportGUI(){
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}
首先,调用reportInterface
,这将初始化您的框架...
其次,调用allReportsBtn
,这将创建您的按钮...
第三,调用comboBoxes
,它将List
的内容应用于您的组合框,该组合框为空...
第四,调用fileRead
,从文件中读取值...
提供给JComboBox
与读取的文件的值List
之间没有关系,因此,即使您告诉组合框值已更改,也不会看到该更改
尝试做更多类似的事情
public ReportGUI(){
reportInterface();
allReportsBtn();
fileRead();
comboBoxes();
}
代替...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.