gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:50:19 25 4
gpt4 key购买 nike

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);
}

}
}

最佳答案

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

我不明白你的代码是这样的

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

我希望有这样的事情

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

关于java - 如何通过迭代具有数组值的 for 循环来填充 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com