import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class LongListTest extends JFrame implements ListSelectionListener {
JLabel label = new JLabel();
public LongListTest() {
setTitle("LongListTest");
setSize(400, 300);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JList wordList = new JList(new WordListModel(300000));
wordList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
wordList.setFixedCellWidth(50);
wordList.setFixedCellHeight(15);
JScrollPane scrollPane = new JScrollPane(wordList);
JPanel p = new JPanel();
p.add(scrollPane);
wordList.addListSelectionListener(this);
getContentPane().add(p, "Center");
getContentPane().add(label, "North");
}
public void valueChanged(ListSelectionEvent evt) {
JList source = (JList) evt.getSource();
String word = (String) source.getSelectedValue();
label.setText(word);
}
public static void main(String[] args) {
JFrame frame = new LongListTest();
frame.show();
}
}
class WordListModel extends AbstractListModel {
private int length;
public WordListModel(int n) {
length = n;
}
public int getSize() {
return length;
}
public Object getElementAt(int n) {
return Integer.toString(n);
}
}
|