将文本导入JTable - 行不是分开的,它们只是创建一个非常长的行。 - Java Swing

时间:2014-06-08 04:06:52

标签: java swing jtable

我创建了一个由各种arraylists填充的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;

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");
//        
//  }
}
编辑:我添加了一个循环但它现在没有显示该表应该是什么。只是空白。

  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 个答案:

答案 0 :(得分:3)

现在变量“i”的值总是为“0”,所以你只有一行数据。

您不能只将ArrayList添加到TableModel。您需要将ArrayList中的每个项目单独添加到TableModel。您所看到的可能是表格单元格中每个ArrayList的“toString()”表示。

如果你想要多行,那么你需要一个循环来填充多行数据。因此,您需要循环遍历ArrayLists中的所有值,并为每个循环增加“i”变量。

另外,更好的设计是不要创建100 x 100的2Dimension数组。这只会创建不包含任何值的额外数据行。相反,您应该使用addRow(...)的{​​{1}}方法在表格中添加一行数据。

  

行不分离,它们只是创建一个非常长的行。 - Java Swing

您看到长行的原因是您将表定义为包含100行和100列,这是不合理的,因为您只有6列数据。我不知道有多少行,但这就是为什么你应该使用addRow()方法而不是试图硬编码TableModel的大小。

代码应该是这样的:

DefaultTableModel

然后你创建一个循环:

DefaultTableModel model = new DefaultTableModel(columnName, 0);