Java by API/javax.swing/ListModel
implements ListModel
//
import java.awt.ruponent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
public class MainClass extends JFrame {
MainClass(String s) {
super(s);
ListModel lm = new StaticListModel();
JList list = new JList();
list.setModel(lm);
list.setCellRenderer(new MyCellRenderer());
getContentPane().add(new JScrollPane(list));
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] s) {
MainClass l = new MainClass("ListModel");
l.pack();
l.setVisible(true);
}
class MyCellRenderer extends JLabel implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
Component c = null;
if (value == null) {
c = new JLabel("(null)");
} else if (value instanceof Component) {
c = (Component) value;
} else {
c = new JLabel(value.toString());
}
if (isSelected) {
c.setBackground(list.getSelectionBackground());
c.setForeground(list.getSelectionForeground());
} else {
c.setBackground(list.getBackground());
c.setForeground(list.getForeground());
}
if (c instanceof JComponent) {
((JComponent) c).setOpaque(true);
}
return c;
}
}
class StaticListModel implements ListModel {
private final Object[] data = { "Hello", new Object(), new java.util.Date(),
new JLabel("Hello world!"), this, };
public Object getElementAt(int index) {
return data[index];
}
public int getSize() {
return data.length;
}
public void addListDataListener(ListDataListener ldl) {
}
public void removeListDataListener(ListDataListener ldl) {
}
}
}
ListModel: getElementAt(int index)
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class MainClass {
public static void main(final String args[]) {
final String labels[] = { "A", "B", "C", "D", "E" };
JFrame frame = new JFrame("Selecting JList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList jlist = new JList(labels);
JScrollPane scrollPane1 = new JScrollPane(jlist);
frame.add(scrollPane1);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2) {
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
};
jlist.addMouseListener(mouseListener);
frame.setSize(350, 200);
frame.setVisible(true);
}
}