小编典典

将文本导入JTable-行不会分开,它们只是创建一个很长的行。-Java秋千

java

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




     }
     }

阅读 207

收藏
2020-11-30

共1个答案

小编典典

现在,变量“ i”的值始终为“ 0”,因此您只有一行数据。

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

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

另外,更好的设计是不要创建100 x
100的2维数组。这只会创建多余的不包含值的数据行。相反,您应该使用的addRow(...)方法DefaultTableModel来一次向表中添加一行数据。

行不是分开的,它们只是创建单个很长的行。-Java秋千

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

该代码应类似于:

DefaultTableModel model = new DefaultTableModel(columnName, 0);

然后创建一个循环:

String[] row = new String[6];
row[0] = fileManage.getNameList().get(i);
row[1] = fileManage.getCourseList().get(i);
...
model.addRow( row );
2020-11-30