我有一个 JList 组件,我想存储图像的路径,但要显示图像的名称,而不是路径。我有一个像这样按下打开文件按钮的代码:
public void openButtonActionPerformed() {
int returnVal = fc.showDialog(null, "Open");
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
}
jList1.setCellRenderer(new JavaRenderer());
}
和:
class JavaRenderer extends DefaultListCellRenderer {
public JavaRenderer(){
//setOpaque(true);
}
public File file;
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean hasFocus) {
if (value instanceof File) {
File file = (file) value;
return super.getListCellRendererComponent(list, file.getName(), index, isSelected, hasFocus);
}else {
return super.getListCellRendererComponent(list, value, index, isSelected, hasFocus);
}
}
}
但它仍然不起作用。你能帮我解决这个问题吗?