import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
class MyListModel extends DefaultListModel {
protected int[] count = new int[100];
public Object getElementAt(int index) {
if (index < 100) {
count[index]++;
}
return super.elementAt(index);
}
public void dump() {
for (int i = 0; i < 100; i++) {
if (count[i] != 0) {
System.out.println(i + "\t" + count[i] + "\t\t" + elementAt(i));
}
}
}
}
public class Main extends JFrame {
MyListModel model = new MyListModel();
public Main() {
for (int i = 0; i < 25; i++) {
model.addElement("A" + i);
}
JList list = new JList(model);
getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
model.dump();
System.exit(0);
}
});
pack();
setVisible(true);
}
public static void main(String arg[]) {
new Main();
}
}
|