/*
* Created on 03.03.2005@14:43:12
*
* TODO Some Licence info...
*/
package de.tutorials;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
/**
* @author TDarimont
*
* TODO Explain me
*/
public class ComboBoxExample extends JFrame {
private JComboBox box;
public ComboBoxExample() {
super("ComboBoxExample");
setDefaultCloseOperation(EXIT_ON_CLOSE);
box = new JComboBox(TimeOfDay.TIMES_OF_DAY.toArray());
box.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean cellHasFocus) {
if (index == -1) {
return super.getListCellRendererComponent(list, value,
index, isSelected, cellHasFocus);
}
String newValue = (index + 1) + ". " + value;
return super.getListCellRendererComponent(list, newValue,
index, isSelected, cellHasFocus);
}
});
getContentPane().add(box);
pack();
setVisible(true);
}
public static void main(String[] args) {
new ComboBoxExample();
}
static class TimeOfDay {
public final static String MORNING = "MorgensAA";
public final static String NOON = "MittagsBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
public final static String EVENINGS = "AbendsCCC";
public final static List TIMES_OF_DAY = Collections
.unmodifiableList(new ArrayList() {
{
add(MORNING);
add(NOON);
add(EVENINGS);
}
});
}
}