Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement JDatePicker

I am working with the latest release 1.3.4 of JDatePicker. How should it be implemented?

I get a compiler error: The constructor JDatePanelImpl(UtilDateModel) is undefined. The suggested fix is to: add argument to match JDatePanelImpl(DateModel, Properties). What should be passed in as the properties argument?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import java.util.Calendar;
import java.util.Date;
import org.jdatepicker.impl.*;
import org.jdatepicker.util.*;
import org.jdatepicker.*;

    //import org.jdatepicker.graphics.*;
class date2 {

    void GUI() {
        JFrame f1 = new JFrame();
        f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f1.setSize(300, 300);
        f1.setVisible(true);

        Container conn = f1.getContentPane();
        conn.setLayout(null);

        UtilDateModel model = new UtilDateModel();
        //model.setDate(20,04,2014);
        JDatePanelImpl datePanel = new JDatePanelImpl(model);
        JDatePickerImpl datePicker = new JDatePickerImpl(datePanel);
        f1.add(datePicker);

    }

}

...Runner...

class testDate2 {

    public void main(String[] args) {

        date2 d1 = new date2();
        d1.GUI();

    }

}
like image 543
Ashane Alvis Avatar asked Nov 07 '14 05:11

Ashane Alvis


People also ask

How do I add a calendar to my swing?

Step1:- Select Tools->Palette->Swing/AWT Components Step2:- Click 'Add from JAR'in Palette Manager Step3:- Browse to [NETBEANS HOME]\ide\modules\ext and select swingx-0.9.


1 Answers

Assuming you are using 1.3.4, then the constructor requirements have changed...

UtilDateModel model = new UtilDateModel();
//model.setDate(20,04,2014);
// Need this...
Properties p = new Properties();
p.put("text.today", "Today");
p.put("text.month", "Month");
p.put("text.year", "Year");
JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
// Don't know about the formatter, but there it is...
JDatePickerImpl datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());

enter image description here

Using this AbstractFormatter...

public class DateLabelFormatter extends AbstractFormatter {

    private String datePattern = "yyyy-MM-dd";
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

    @Override
    public Object stringToValue(String text) throws ParseException {
        return dateFormatter.parseObject(text);
    }

    @Override
    public String valueToString(Object value) throws ParseException {
        if (value != null) {
            Calendar cal = (Calendar) value;
            return dateFormatter.format(cal.getTime());
        }

        return "";
    }

}
like image 147
MadProgrammer Avatar answered Oct 21 '22 20:10

MadProgrammer