我正在尝试使用AbstractTableModel创建一个显示temerary文件夹中所有文件的程序,但是如何编写getValueAt()方法。 文件名应位于第一列,文件路径应位于第二列。 我现在把它搞糊涂了,但有人可以告诉我锄头我应该编码吗?
这是我到目前为止所得到的:
public class UI extends JFrame {
private JPanel contentPane;
private JTable table;
File[] dir = new File(System.getProperty("java.io.tmpdir")).listFiles();
public static void main(String[] args) {
public UI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 542, 422);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
tabbedPane.setBounds(0, 0, 526, 384);
contentPane.add(tabbedPane);
JPanel panel = new JPanel();
tabbedPane.addTab("Temp Files", null, panel, null);
panel.setLayout(null);
final AbstractTableModel myAbstractTableModel = new AbstractTableModel() {
@Override
public int getColumnCount() {
return 2;
}
@Override
public int getRowCount() {
return dir.length;
}
@Override
public Object getValueAt(int arg0, int arg1) {
return null;
}
};
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 402, 334);
panel.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
table.setModel(myAbstractTableModel);
}
}
答案 0 :(得分:1)
在您的示例中,arg0
是row
而arg1
是col
,因此dir[arg0]
是每个{File
row
1}}。在getValueAt()
的实施中,请返回file.getName()
或file.getPath()
,如col
值所示。 EnvTableTest
是一个使用更具描述性的参数名称的相关示例。