我有一个 Java 应用程序,它使用 JTables 添加文件以供浏览。它仅包含 1 列:
DefaultTableModel model = new DefaultTableModel(new String[] {"Name"}, 0);
JTable tracks = new JTable(model){
public boolean isCellEditable(int rowIndex, int collIndex){
return false;
}
};
tracks.addMouseListener(new PopupListener());
tracks.setShowGrid(false);
将文件添加到表中的操作如下:
for (File file : results){
model.addRow(new Object[] {file});
}
我需要添加实际文件,而不仅仅是其名称,因为稍后我将使用它。但是,表格是否有可能只显示文件名(file.getName();)而不是整个路径?
最佳答案
..is it possible that the table shows only the file name (file.getName();) instead of the entire path?
确实是这样。使用TableCellRenderer
仅显示名称。请参阅How to Use Tables - Concepts: Editors and Renderers了解详情。
另请参阅File Browser GUI了解一般提示(例如使用文件的图标)。
关于java - JTable添加File对象,但只显示文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29580706/