..它是一个文件选择器,所以我想优先显示文件的结尾。
我建议将其作为替代方案,或者将工具提示(路径)放在文件名之后。
import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
class FileListName {
final class FileListCellRenderer extends DefaultListCellRenderer {
private FileSystemView fsv = FileSystemView.getFileSystemView();
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
JLabel l = (JLabel) c;
File f = (File) value;
l.setText(f.getName());
l.setIcon(fsv.getSystemIcon(f));
l.setToolTipText(f.getAbsolutePath());
return l;
}
}
public FileListName() {
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog(null);
if (jfc.getSelectedFile() != null) {
File[] f = {jfc.getSelectedFile()};
JList list = new JList(f);
list.setVisibleRowCount(1);
list.setCellRenderer(new FileListCellRenderer());
JOptionPane.showMessageDialog(null, list);
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
// Provides better icons from the FSV.
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
new FileListName();
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}