Reputation: 311
I am still playing with Java and swing (still quite new to it all). I am trying to populate a JComboBox with the data from a .txt file. I am pulling the data into an ArrayList and am attempting to populate the JComboBox with the ArrayList variable. The combobox is blank when I run the application however.
This is the Array Code:
private ArrayList<String> list = new ArrayList<String>();
File Reader Code:
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();
}
}
And the mess I am using for my comboboxes:
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);
}
Entire mess of code for this class.
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);
}
}
Any ideas where I am going wrong?
Upvotes: 0
Views: 423
Reputation: 347184
Taka close look at the order you are doing thins...
public ReportGUI(){
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}
First, you call reportInterface
, which initialises your frame...
Second, you call allReportsBtn
, which creates your buttons...
Third, you call comboBoxes
, which applies the contents of the List
to your combo box, which is empty...
Fourth, you call fileRead
, which reads the values from the file...
There is no relationship between the array you supplied to the JComboBox
or the List
of values you read of the file, so even if you told the combobox that the values had changed, it would not see that change
Try doing something more like
public ReportGUI(){
reportInterface();
allReportsBtn();
fileRead();
comboBoxes();
}
instead...
Upvotes: 2
Reputation: 324098
public ReportGUI()
{
reportInterface();
allReportsBtn();
comboBoxes();
fileRead();
}
As Obicere has pointed out:
comboBoxes()
, which creates the combo boxes and populates the combo box with an empty List.fileRead()
method which adds the data to the List, but this does NOT update the model of the combo box.The code order needs to be reversed:
fileRead();
comboBoxes();
Upvotes: 3