簡體   English   中英

更改JComboBox的標簽

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM