尝试将tablecellrenderer
应用于column.I需要有三个不同的图标,分别用于文件夹,zip \ rar文件和其他文件。
我如何设置它们?
如果条款(if f.isDirectory())
根本不起作用。
public class FirstCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
File f = (File) value;
ImageIcon folderIcon = new ImageIcon(getClass().getResource("/images/folder.png"));
ImageIcon archiveIcon = new ImageIcon(getClass().getResource("/images/archive.png"));
if (!isSelected) {
label.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
}
label.setIcon(f.isDirectory() ? folderIcon : archiveIcon);
label.setText(f.getName());
return label;
}
}
答案 0 :(得分:1)
为什么你没有得到你期望的结果可能有很多原因,但是你提供的这些信息很少,我们可以通过这种猜测来诊断问题。
您可能有 - 忘记向表格注册渲染器,或者使用错误的类型或列进行注册
你可能有 - 不将图像放在正确的位置
你可能有 - 你的文件列表中没有目录。
以下是一个简单的示例,它演示了渲染器的核心概念是否正常工作,必然会有其他缺失......
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TestTable100 {
public static void main(String[] args) {
new TestTable100();
}
public TestTable100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Files");
for (File file : new File(System.getProperty("user.home")).listFiles()) {
model.addRow(new Object[]{file});
}
JTable table = new JTable(model);
table.getColumnModel().getColumn(0).setCellRenderer(new FirstCellRenderer());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class FirstCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = (JLabel) super.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
File f = (File) value;
// ImageIcon folderIcon = new ImageIcon(getClass().getResource(
// "/images/folder.png"));
// ImageIcon archiveIcon = new ImageIcon(getClass().getResource(
// "/images/archive.png"));
if (!isSelected) {
label.setBackground(row % 2 == 0 ? Color.LIGHT_GRAY : Color.WHITE);
}
// label.setIcon(f.isDirectory() ? folderIcon : archiveIcon);
String prefix = f.isDirectory() ? "[d]" : "[f]";
label.setText(prefix + " " + f.getName());
return label;
}
}
}
更新以不同方式显示Zip文件
请注意File
API不能很好地处理某些“特殊”文件,例如符号链接或Windows等价物(我看着你的音乐)
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TestTable100 {
public static void main(String[] args) {
new TestTable100();
}
public TestTable100() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Files");
for (File file : new File(System.getProperty("user.home")).listFiles()) {
model.addRow(new Object[]{file});
}
JTable table = new JTable(model);
table.getColumnModel().getColumn(0).setCellRenderer(new FirstCellRenderer());
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class FirstCellRenderer extends DefaultTableCellRenderer {
protected static ImageIcon FOLDER_ICON;
protected static ImageIcon FILE_ICON;
protected static ImageIcon ZIP_FILE_ICON;
static {
try {
FOLDER_ICON = new ImageIcon(ImageIO.read(FirstCellRenderer.class.getResource("/Folder.png")));
} catch (IOException exp) {
FOLDER_ICON = null;
exp.printStackTrace();
}
try {
FILE_ICON = new ImageIcon(ImageIO.read(FirstCellRenderer.class.getResource("/File.png")));
} catch (IOException exp) {
FILE_ICON = null;
exp.printStackTrace();
}
try {
ZIP_FILE_ICON = new ImageIcon(ImageIO.read(FirstCellRenderer.class.getResource("/ZipFile.png")));
} catch (IOException exp) {
ZIP_FILE_ICON = null;
exp.printStackTrace();
}
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
File f = (File) value;
setIcon(null);
if (f.isDirectory()) {
setIcon(FOLDER_ICON);
} else {
String name = f.getName();
if (name.toLowerCase().endsWith(".zip")) {
setIcon(ZIP_FILE_ICON);
} else {
setIcon(FILE_ICON);
}
}
setText(f.getName());
return this;
}
}
}
答案 1 :(得分:0)
试试这些
label.setIcon(f.getParent()==null ? folderIcon : archiveIcon);
//label.setIcon(f.getParentFile()==null ? folderIcon : archiveIcon);
代替下面的行
label.setIcon(f.isDirectory() ? folderIcon : archiveIcon);