如何通过迭代具有数组值的for循环来填充JTable

时间:2014-07-08 16:37:23

标签: java swing file-io jtable

enter image description here

我希望在上图中获得外观和感觉。

这是我写的代码,但我不知道如何使它看起来像这张表。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import static javax.swing.JFrame.EXIT_ON_CLOSE;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.xml.crypto.Data;

import com.cdac.FileNameCopy;

class TableCheckBox extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public void TableCheckBoxfunc(String fileName,String Ext,boolean selection) 
    {
        Object[] columnNames = {"File Names", "Extentions","Selection"};
        Object[][] data = { {fileName,Ext,selection}};

        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

          @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:   return String.class;
                    case 1:   return String.class;
                    default:  return Boolean.class;
                }
            }
        };

        Container c = getContentPane();
        c.setLayout(new FlowLayout());
        c.add(new JScrollPane(table), BorderLayout.CENTER);


        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);


        return ;
    }

    public static void main(String[] args) 
    {

        boolean selection=false;
        String fileName="";
        String extension="";


        File folder = new File("c:\\file");
        File[] listOfFiles = folder.listFiles();


        TableCheckBox frame = new TableCheckBox();

        for (int i = 0; i < listOfFiles.length; i++) 
        {
              if (listOfFiles[i].isFile()) {

                  final String fullName=listOfFiles[i].getName();
                  int a = fullName.lastIndexOf(".");
                  int len=fullName.length();

                  fileName=fullName.substring(0,a);
                  extension=fullName.substring(a+1,len);

                  System.out.println("name of file is :"+fileName);
                  System.out.println("Extension of file is :"+extension);




                System.out.println("File " + fullName);
              } else if (listOfFiles[i].isDirectory()) {
                System.out.println("Directory " + listOfFiles[i].getName());
              }
              frame.TableCheckBoxfunc(fileName, extension, selection);
                // frame.TableCheckBoxfunc(fileName, extension, selection);;
              frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
              frame.pack();
              frame.setLocation(150, 150);
              frame.setVisible(true);
        }

    }
}

2 个答案:

答案 0 :(得分:1)

这将非常有帮助。 http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

我在您的代码中无法理解的是

Object[][] data = { {fileName,Ext,selection}};

我希望这样的事情

Object[][] data = {
    {"S001", "Alice", 90, true}
    //...more rows
}

答案 1 :(得分:1)

首先填充模型数据Object[][],然后最后将其添加到DefaultTableModel并仅创建JTable一次。

示例代码:

Object[][] data = new Object[listOfFiles.length][3];
for (int i = 0; i < listOfFiles.length; i++) {
    ...
    data[i][0] = fileName;
    data[i][1] = extension;
    data[i][2] = selection;
}

Object[] columnNames = { "File Names", "Extentions", "Selection" };
DefaultTableModel model = new DefaultTableModel(data, columnNames);
// create JTable just once and pass model in the constructor

如何使用表格? learn more...