[英]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.