您可以創建文檔的包裝。例如,包裝器可以暴露數據和名稱。如果需要,數據也可以根據需要加載。將包裝的實例添加到列表中。使用文檔名稱來呈現列表中的每個條目。選擇列表中的條目後,使用所選項目進行處理。這是一個簡單的例子,顯示文件列表。一旦選擇了文檔,其內容將顯示在文本區域中。
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
public class TestListModel {
public TestListModel() {
DefaultListModel model = new DefaultListModel();
model.addElement(new DocumentWrapper("Doc 1", "Doc 1 data"));
model.addElement(new DocumentWrapper("Doc 2", "Doc 2 data"));
model.addElement(new DocumentWrapper("Doc 3", "Doc 3 data"));
final JList list = new JList();
final JTextArea output = new JTextArea(5, 40);
list.setModel(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setCellRenderer(new DocumentWrapperRenderer());
list.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
DocumentWrapper docWrapper = (DocumentWrapper) list
.getSelectedValue();
if (docWrapper != null) {
output.setText(docWrapper.getData());
} else {
output.setText("");
}
}
}
});
JFrame frame = new JFrame("Document list");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(list));
frame.add(output, BorderLayout.SOUTH);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public class DocumentWrapper {
private String data;
private String name;
public DocumentWrapper(String name, String data) {
this.name = name;
this.data = data;
}
public String getData() {
return data;
}
public String getName() {
return name;
}
}
public static class DocumentWrapperRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
if (value instanceof DocumentWrapper) {
return super.getListCellRendererComponent(
list, ((DocumentWrapper) value).getName(), index,
isSelected, hasFocus);
}
return super.getListCellRendererComponent(list, value, index,
isSelected, hasFocus);
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestListModel();
}
});
}
}
非常有幫助的例子 – programmer