How to use jXDatePicker with maskFormatter?
Asked Answered
A

2

17

I would like to use a jxdatepicker with maskFormatter. I tried

MaskFormatter maskFormatter = new MaskFormatter ("##/##/####");
JFormattedTextField field=new JFormattedTextField (maskFormatter);
jXDatePicker.setEditor (field);

and

MaskFormatter maskFormatter = new MaskFormatter ("##/##/####");
maskFormatter.install (jXDatePicker.getEditor ());

neither the first nor the second solution worked

PS: A JFormattedTextField work fine with MaskFormatter AND jXDatePicker work fine with a simple JFormattedTextField

Atkins answered 26/4, 2016 at 8:47 Comment(2)
Can you explain more on what are you expecting, and what is not workingAllhallowmas
I want to do the same thing in the date input in this page link but in swing with JXDatePicker If I use a JFormattedTextField with MaskFormatter i got the expected result, but I want to use JXDatePicker so the user can select the date withe the mouse or type it withe the keyboard, And as I said in the question the MaskFormatter doesn't work with JXDatePickerAtkins
M
1

This is an old question, but seems to be still active, so here is how we implemented the functionality some time ago (swingx-all-1.6.5-1.jar):

1) Create a wrapper class for MaskFormatter

public class Wrapper extends MaskFormatter {

private final static String DD_MM_YYY = "dd/MM/yyyy";

public Wrapper(String string) throws ParseException {
    super(string);

}

@Override
public Object stringToValue(String value) throws ParseException {

    SimpleDateFormat format = new SimpleDateFormat(DD_MM_YYY);
    Date parsed = format.parse(value);
    return parsed;

}

public String valueToString(Object value) throws ParseException {
    if (value != null) {
        SimpleDateFormat format = new SimpleDateFormat(DD_MM_YYY);
        String formated = format.format((Date) value);
        return super.valueToString(formated);
    } else {
        return super.valueToString(value);
    }

  }

}


2) Add the wrapped Formatter to the JFormattedTextField and set it on the JXDatePicker

MaskFormatter maskFormatter;
JXDatePicker datePicker = new JXDatePicker();
try {
        maskFormatter = new Wrapper("##/##/####");
        JFormattedTextField field = new JFormattedTextField(maskFormatter);
        datePicker.setEditor(field);
} catch (ParseException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
}
somePanel.add(datePicker);

The wrapper class basically does the formatting, since trying to set a DateFormat on the JXDatePicker led to various ParseException.

Money answered 21/11, 2018 at 17:25 Comment(0)
C
0

Personally I'm not very skilled in Java but after checking some docs quickly. I think setEditor is not the way to go. With maskFormatter.install you seem to go into the right direction. Something like this might help you out:

JXDatePicker picker = new JXDatePicker();
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
picker.setFormats(format);

Selective source: JXDatePicker using SimpleDateFormat to format dd.MM.yy to dd.MM.yyyy with current century

Or check out this: https://mcmap.net/q/747059/-jxdatepicker-using-simpledateformat-to-format-dd-mm-yy-to-dd-mm-yyyy-with-current-century

Courthouse answered 10/5, 2016 at 9:44 Comment(3)
Nop that will only format the date that way, but the user still has to type the / character, what i want is somthing like the date input in this page linkAtkins
@Atkins so what about the DatePickerFormatter formatter = new DatePickerFormatter( code from https://mcmap.net/q/747059/-jxdatepicker-using-simpledateformat-to-format-dd-mm-yy-to-dd-mm-yyyy-with-current-century ?Courthouse
Maybe @kleopatra is able to help you with this one.Courthouse

© 2022 - 2024 — McMap. All rights reserved.