问题
I have created a JTable which is populated by various arraylists. The issue I am having is that the data (all of it) is only going into the top row.
Am I supposed to call some sort of row delimiter or something?
The code is below:
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;
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 = new JTable();
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();
}
private void table(){
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;
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");
//
// }
}
EDIT: I added a loop but it is now showing nothing where the table should be. Just blank space.
private void table(){
String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"};
DefaultTableModel model = new DefaultTableModel(colName,0);
//String[] row = new String[6];
for(Object item: nameList){
Object[] row = new Object[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);
}
}
回答1:
Right now the variable "i" always has the value of "0", so you only have a single row of data.
You can't just add an ArrayList to the TableModel. You need to add each item from the ArrayList individually to the TableModel. What you are seeing is probably the "toString()" representation of each ArrayList in a cell of the table.
If you want multiple rows then you need a loop to populate multiple rows of data. So you need to loop through all the values in your ArrayLists and increment the "i" variable for each loop.
Also, a better design is to not create a 2Dimension Array of 100 x 100. This will just create extra rows of data that contain no values. Instead you should use the addRow(...) method of the DefaultTableModel to add one row of data at to time to the table.
Rows aren't separating, they are just creating a single very long row. - Java Swing
The reason you see a long row is because you define the table to contain 100 rows and 100 columns, which is not reasonable since you only have 6 columns of data. I don't know how many rows, but that is why you should use the addRow() method and not try to hard code the size of the TableModel.
The code should be something like:
DefaultTableModel model = new DefaultTableModel(columnName, 0);
Then you create a loop:
String[] row = new String[6];
row[0] = fileManage.getNameList().get(i);
row[1] = fileManage.getCourseList().get(i);
...
model.addRow( row );
来源:https://stackoverflow.com/questions/24102895/importing-text-into-jtable-rows-arent-separating-they-are-just-creating-a-si