15

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();

    }

}
dshgna
  • 812
  • 1
  • 15
  • 34
Ashane Alvis
  • 730
  • 2
  • 20
  • 41

2 Answers2

46

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 "";
    }

}
MadProgrammer
  • 336,120
  • 22
  • 219
  • 344
  • Yes i'm using Jdatepicker 1.3.4, as to test this i have use a null layout. This is for our final java project for the diploma level. Thank you for the help. Also can you suggest a way to add background image for the JFrame/JPanel? – Ashane Alvis Nov 07 '14 at 18:06
  • 1
    The best way is to create a custom component extending from JPanel, override its paintComponent method and draw the image there. Search SO, there are plenty of examples – MadProgrammer Nov 07 '14 at 20:08
  • How do I get the date to a JLabel or Jtextfield? – Ashane Alvis Nov 08 '14 at 10:09
  • have you tried `getDate`? You'll probably want to use a `DateFormatter` and/or a `JFormattedField` – MadProgrammer Nov 08 '14 at 10:30
  • 1
    There's also a `JDateComponentFactory` which creates `JDatePickerImpl` and `JDatePanelImpl` objects, with the models and i18n properties already set. I'm a little unclear on whether the `JDatePanelImpl` constructor is intended to be called directly by clients at this point, since there's not great guidance in the docs on how to construct the i18n `Properties` it's using. – Andrew Janke May 07 '15 at 22:02
  • @AndrewJanke I went looking for something like that, but couldn't find anything (useful) and the only example I could find that worked used the implementation directly. I agree, it's a bit dirty and I wasn't happy doing so – MadProgrammer May 07 '15 at 22:07
  • Yeah, I had to dig around in the source to learn how to use it, since it's not documented anywhere—all the examples online are using the old pre-1.3.4 API. Usage: `JDatePanelImpl datePanel = (JDatePanelImpl) new JDateComponentFactory().createJDatePanel(); UtilCalendarModel model = (UtilCalendarModel) datePanel.getModel();`. This is a bit dirty in its own way, but I'm happier doing this than mucking around with the i18n Properties directly. – Andrew Janke May 07 '15 at 22:21
  • 2
    @AndrewJanke Yeah, it's still a bunch of hacking which shouldn't be needed. I can't see anyway to set the model for a `JDatePanel` without casting it and you shouldn't have to cast it because that's the point of the factory and ... I'm going to use `JXDatePicker` instead :P – MadProgrammer May 07 '15 at 23:54
  • `AbstractFormatter` is serializable, so the `DateLabelFormatter` needs to either `@SuppressWarnings("serial")` or include `private static final long serialVersionUID`. – Nick Graham Nov 11 '16 at 22:40
  • 1
    @NickGraham You know, in 16 years I've never "had" to include `serialVersionUID`, but then again, I don't use Eclipse, nor am I an exponent of Serialisation, as there are better solutions for most problems, but that's just me – MadProgrammer Nov 11 '16 at 23:13
6

Just use properties in the constructor of JDatePanelImpl

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);
Robert
  • 5,231
  • 43
  • 62
  • 114
Ankur Saxena
  • 61
  • 1
  • 2