I have created a JTable which is populated by various arraylists which get their data from a text list using a "~" to split. The issue I am having is that the table is displaying all data from the list on a single row. For example:
Column1 Column2 Column2 Column2 Column3 Column4
1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5
When I want it to display
Column1 Column2 Column2 Column2 Column3 Column4
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
You get the idea. From previous advice, I think the issue may be looping, but I am not sure.
Any advice would be great.
The code is below:
private void table(){
String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"};
DefaultTableModel model = new DefaultTableModel(colName,0);
for(Object item : courseList){
Object[] row = new Object[6];
// String[] row = new String[6];
row[0] = fileManage.getCourseList();
row[1] = fileManage.getNameList();
row[2] = fileManage.getModeratorList();
row[3] = fileManage.getSemesterList();
row[4] = fileManage.getProgramList();
row[5] = fileManage.getMajorList();
model.addRow(row);
textArea = new JTable(model);
}
This is the class that has the arraylists:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class FileIOManagement {
private ArrayList<String> nameList = new ArrayList<String>();
private ArrayList<String> courseList = new ArrayList<String>();
private ArrayList<String> semesterList = new ArrayList<String>();
private ArrayList<String> moderatorList = new ArrayList<String>();
private ArrayList<String> programList = new ArrayList<String>();
private ArrayList<String> majorList = new ArrayList<String>();
public ArrayList<String> getNameList(){
return this.nameList;
}
public ArrayList<String> getCourseList(){
return this.courseList;
}
public ArrayList<String> getSemesterList(){
return this.semesterList;
}
public ArrayList<String> getModeratorList(){
return this.moderatorList;
}
public ArrayList<String> getProgramList(){
return this.programList;
}
public ArrayList<String> getMajorList(){
return this.majorList;
}
public void setNameList(ArrayList<String> nameList){
this.nameList = nameList;
}
public void setCourseList(ArrayList<String> courseList){
this.courseList = courseList;
}
public void setSemesterList(ArrayList<String> semesterList){
this.semesterList = semesterList;
}
public void setModeratorList(ArrayList<String> moderatorList){
this.moderatorList = moderatorList;
}
public void setProgramList(ArrayList<String> programList){
this.programList = programList;
}
public void setMajorList(ArrayList<String> majorList){
this.majorList = majorList;
}
public FileIOManagement(){
setNameList(new ArrayList<String>());
setCourseList(new ArrayList<String>());
setSemesterList(new ArrayList<String>());
setModeratorList(new ArrayList<String>());
setProgramList(new ArrayList<String>());
setMajorList(new ArrayList<String>());
readTextFile();
getNameList();
getCourseList();
}
private void readTextFile(){
try{
Scanner scan = new Scanner(new File("Course.txt"));
while(scan.hasNextLine()){
String line = scan.nextLine();
String[] tokens = line.split("~");
String course = tokens[0].trim();
String examiner = tokens[1].trim();
String moderator = tokens[2].trim();
String semester = tokens[3].trim();
String program = tokens[4].trim();
String major = tokens[5].trim();
courseList.add(course);
semesterList.add(semester);
nameList.add(examiner);
moderatorList.add(moderator);
programList.add(program);
majorList.add(major);
HashSet hs = new HashSet();
hs.addAll(nameList);
nameList.clear();
nameList.addAll(hs);
Collections.sort(nameList);
}
scan.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
This is the class where I need to have the JTable:
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
public class AllDataGUI extends JFrame{
private JButton saveCloseBtn = new JButton("Save Changes and Close");
private JButton closeButton = new JButton("Exit Without Saving");
private JFrame frame=new JFrame("Viewing All Program Details");
private final FileIOManagement fileManage = new FileIOManagement();
private ArrayList<String> nameList = new ArrayList();
private ArrayList<String> courseList = new ArrayList();
private ArrayList<String> semesterList = new ArrayList();
private ArrayList<String> moderatorList = new ArrayList();
private ArrayList<String> majorList = new ArrayList();
private ArrayList<String> programList = new ArrayList();
private JTable textArea;
public ArrayList<String> getNameList(){
return this.nameList;
}
public ArrayList<String> getCourseList(){
return this.courseList;
}
public ArrayList<String> getSemesterList(){
return this.semesterList;
}
public ArrayList<String> getModeratorList(){
return this.moderatorList;
}
public ArrayList<String> getProgramList(){
return this.programList;
}
public ArrayList<String> getMajorList(){
return this.majorList;
}
public void setNameList(ArrayList<String> nameList){
this.nameList = nameList;
}
public void setCourseList(ArrayList<String> courseList){
this.courseList = courseList;
}
public void setSemesterList(ArrayList<String> semesterList){
this.semesterList = semesterList;
}
public void setModeratorList(ArrayList<String> moderatorList){
this.moderatorList = moderatorList;
}
public void setProgramList(ArrayList<String> programList){
this.programList = programList;
}
public void setMajorList(ArrayList<String> majorList){
this.majorList = majorList;
}
public AllDataGUI(){
getData();
table();
panels();
}
public Object getValueAt(int rowIndex, int columnIndex) {
String[] token = nameList.get(rowIndex).split(",");
return token[columnIndex];
}
private void table(){
String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"};
DefaultTableModel model = new DefaultTableModel(colName,0);
for(Object item : courseList){
Object[] row = new Object[6];
// String[] row = new String[6];
row[0] = fileManage.getCourseList();
row[1] = fileManage.getNameList();
row[2] = fileManage.getModeratorList();
row[3] = fileManage.getSemesterList();
row[4] = fileManage.getProgramList();
row[5] = fileManage.getMajorList();
model.addRow(row);
textArea = new JTable(model);
// String END_OF_LINE = ",";
//
// String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"};
//// textArea.getTableHeader().setBackground(Color.WHITE);
//// textArea.getTableHeader().setForeground(Color.BLUE);
// // Font Tablefont = new Font("Details", Font.BOLD, 12);
// // textArea.getTableHeader().setFont(Tablefont);
// Object[][] object = new Object[100][100];
// int i = 0;
// if (fileManage.size() != 0) {
// for (fileManage book : fileManage) {
// object[i][0] = fileManage.getCourseList();
// object[i][1] = fileManage.getNameList();
// object[i][2] = fileManage.getModeratorList();
// object[i][3] = fileManage.getSemesterList();
// object[i][4] = fileManage.getProgramList();
// object[i][5] = fileManage.getMajorList();
//
// textArea = new JTable(object, colName);
// }
// }
}
}
public void getData(){
nameList = fileManage.getNameList();
courseList = fileManage.getCourseList();
semesterList = fileManage.getSemesterList();
moderatorList = fileManage.getModeratorList();
majorList = fileManage.getMajorList();
programList = fileManage.getProgramList();
// textArea.(write());
}
private JButton getCloseButton(){
return closeButton;
}
private void panels(){
JPanel panel = new JPanel(new GridLayout(1,1));
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10));
rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10));
JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scrollBarForTextArea);
frame.add(panel);
frame.getContentPane().add(rightPanel,BorderLayout.EAST);
rightPanel.add(saveCloseBtn);
rightPanel.add(closeButton);
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
saveCloseBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//saveBtn();
frame.dispose();
}
});
frame.setSize(1000, 700);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
// private void saveBtn(){
// File file = null;
// FileWriter out=null;
// try {
// file = new File("Course.txt");
// out = new FileWriter(file);
// out.write(textArea.getText());
// out.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// JOptionPane.showMessageDialog(this, "File Successfully Updated");
//
// }
}