Venkat
Venkat

Reputation: 21480

Create a java Date Picker component?

I have the datepicker component in the format of MM/dd/yyyy, But I want to create date picker in java the form of dd/MM/yyyy. So where can I get the suggestion or related solutions. Please give the basic idea about that. Thanks in Advance..

Upvotes: 3

Views: 5850

Answers (4)

Bigger
Bigger

Reputation: 1960

I want to share something I just wrote for my project, I had to make a simple and quick date editor for a JTable that can edit in-place, it can also be used as a normal control. The date format as you can see can be modified switching the order of the string used in SimpleDateFormat, but don't forget to switch the ranges array. Here is the code:

public class DateControl extends JPanel {
    @SuppressWarnings("unchecked")
    private JComboBox<String>[] combos = new JComboBox[3];
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    final int ranges[][] = {{2012,2050},{1,12},{1,31}};
    public DateControl() {
        super();
        combos[0] =  new JComboBox<String>();
        combos[1] =  new JComboBox<String>();
        combos[2] =  new JComboBox<String>();

        // Fill the combos
        for (int i = 0; i<combos.length; i++)
            for (int j = ranges[i][0]; j<ranges[i][1]; j++) 
                combos[i].addItem((j<9?"0":"")+Integer.toString(j));

        this.setLayout(new BoxLayout(this,BoxLayout.X_AXIS));
        for (JComboBox<String> c: combos) {

            // Remove the arrow button from the combo boxes (optional)
            c.setUI(new BasicComboBoxUI() {
                protected JButton createArrowButton() {
                    return new JButton() {
                        public int getWidth() {
                            return 0;
                        }
                    };
                }
            }); 

            this.add(c);
        }

        //This is just for a nice look touch (optional)
        this.setBorder(BorderFactory.createRaisedBevelBorder());

        // Set to today's date
        setDate(new Date());
    }

    // Date argument constructor
    public DateControl(Date date) {
        this();
        setDate(date);
    }

    public void setDate(Date d) {
        String[] date = df.format(d).split("/");
        for (int i=0;i<combos.length; i++)
            combos[i].setSelectedItem(date[i]);
    }

    public Date getDate() {
        String str = combos[0].getSelectedItem()+"/"+combos[1].getSelectedItem()+"/"+combos[2].getSelectedItem();
        Date ret = null;
        try {
            ret = df.parse(str);
        } catch (ParseException e) {e.printStackTrace();}
        return ret;
    }
}

it can then be used as a cell editor like this:

class DateCellEditor extends AbstractCellEditor implements TableCellEditor {
    SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    private DateControl dateControl;

    public DateCellEditor() {
        dateControl = new DateControl();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table,
            Object value,
            boolean isSelected,
            int row,
            int column) {

        Date d = new Date();    

        try {
           Object str = table.getValueAt(row, column);
           if (str!=null)
               d = df.parse((String)str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        dateControl.setDate(d);
        return dateControl;
    }

    @Override
    public Object getCellEditorValue() {
        return df.format(dateControl.getDate());
    }
}

The new cell editor can be used in your table columns like this:

        TableColumn c = myTable.getColumnModel().getColumn(0);
        c.setCellEditor(new DateCellEditor());

I must remind you that this is a cell "Editor", it will only be displayed when the cell is under edition. The date value will be displayed by the normal cell "Renderer" as a simple string with the format yyyy/MM/dd.

I hope all this helps somebody :)

Upvotes: 0

user210158
user210158

Reputation:

The Any+Time™ DatePicker/TimePicker AJAX Calendar Widget allows you to specify dates in whatever format you'd like. It also has WAI/ARIA keyboard support and extensive CSS customization options, including jQuery UI support.

Upvotes: 0

Ravindra Gullapalli
Ravindra Gullapalli

Reputation: 9158

Try JCalendar

you can download it from http://www.toedter.com/en/jcalendar/index.html

Upvotes: 0

chris
chris

Reputation: 9993

in JXDatePicker you can use setFormats to change the date format. see this page and this one.

if you're talking about writing your own component from scratch then you'll need to implement the entire thing, which seems like a bit of a waste of time?

Upvotes: 3

Related Questions