DateComboBoxRenderer.java Source code

Java tutorial

Introduction

Here is the source code for DateComboBoxRenderer.java

Source

import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

class DateComboBoxRenderer extends DefaultListCellRenderer {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        Object item = value;
        if (item instanceof Date) {
            item = dateFormat.format((Date) item);
        }
        return super.getListCellRendererComponent(list, item, index, isSelected, cellHasFocus);
    }
}

public class Main {
    public static void main(String[] str) {
        JComboBox combo = new JComboBox();
        GregorianCalendar calendar = new GregorianCalendar();
        combo.addItem(calendar.getTime());

        calendar.roll(GregorianCalendar.DAY_OF_MONTH, 1);
        combo.addItem(calendar.getTime());

        combo.setRenderer(new DateComboBoxRenderer());

        JFrame frame = new JFrame();

        JPanel panel = new JPanel();
        panel.add(new JLabel("Date Combo: "));
        panel.add(combo);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

}