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

Bob Ortiz
  • 452
  • 1
  • 3
  • 20
Billydan
  • 395
  • 1
  • 7
  • 25
  • Can you explain more on what are you expecting, and what is not working – Madhan May 05 '16 at 14:25
  • I want to do the same thing in the date input in this page [link](http://www.primefaces.org/showcase/ui/input/inputMask.xhtml) 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 `JXDatePicker` – Billydan May 08 '16 at 12:27

2 Answers2

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.

briadeus
  • 550
  • 1
  • 6
  • 13
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://stackoverflow.com/a/9036979/4820655

Community
  • 1
  • 1
Bob Ortiz
  • 452
  • 1
  • 3
  • 20
  • 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 [link](http://www.primefaces.org/showcase/ui/input/inputMask.xhtml) – Billydan May 10 '16 at 10:37
  • @Billydan so what about the `DatePickerFormatter formatter = new DatePickerFormatter(` code from http://stackoverflow.com/a/9036979/4820655 ? – Bob Ortiz May 10 '16 at 10:39
  • Maybe @kleopatra is able to help you with this one. – Bob Ortiz May 10 '16 at 10:42