将包含组件的文件夹插入JList

时间:2009-08-14 16:10:45

标签: java swing

任何人都可以指导我或举例说明如何将组件或组件文件或其文件夹插入JList。这是一个悬而未决的问题,请协助。

1 个答案:

答案 0 :(得分:1)

List<File> files = ... // Obtain files from somewhere ...

// Pass files as an array to JList.  Could alternatively implement custom ListModel.
Object[] arr = files.toArray();   
JList jl = new JList(arr);

// Define renderer to display full file names:
jl.setCellRenderer(new DefaultListCellRenderer() {
  public void getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

    File file = (File)value;
    return super.getListCellRendererComponent(list, file.getPath(), index, isSelected, cellHashFocus);
  }
});