[英]Change the label of JComboBox
我将File
类数组传递给JComboBox
。 这样就可以在GUI中显示所选文件的完整路径。 我只想在组合框中显示文件名。
代码如下:
public void generateGui(File[] files){
// main frame
frame = new JFrame("All In One");
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel scriptPanel = new JPanel();
JCombobox scriptList = new JComboBox(files); // Passing the array of File class
scriptPanel.add(scriptList);
frame.add(scriptPanel,BorderLayout.NORTH);
frame.setVisible(true);
使用ListCellRenderer
仅显示文件名。 尽管给出的是文件,但您可能还希望显示该图标,如本答案所示
另请参阅此答案的FileListCellRenderer
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.