#10 Cannot manually enter null date (in the textfield)

open
nobody
None
5
2014-03-18
2008-04-24
Anonymous
No

To reproduce:

1. create a form that contains DatePicker component
2. click the calendar button and select any date
3. in the textfield, erase the date
4. call commitEdit(); it returns false; does not set the internal Date variable to null
5. call getDate(); it returns the last valid date selected in the calendar component rather than null

Discussion

  • Nobody/Anonymous

    Logged In: NO

    I have a solution for that problem

    Override the BasicDatePickerUI

    public class MyDatePickerUI extends BasicDatePickerUI {

    public static ComponentUI createUI(final JComponent c) {
    return new MyDatePickerUI();
    }

    /** {@inheritDoc} */
    @Override
    protected void installComponents() {
    super.installComponents();
    field.setFormatterFactory(new DefaultFormatterFactory(
    new EmptyDateFormatter(peer.getDateFormat())));
    }

    }
    And reset the formatter on the field. Use the EmptyDateFormatter from JGoodies that returns a null value when an empty string is entered.

    //change the UIManager
    new MicrobaComponent(); //you have to do this, because the first time a MicrobaComponent is created the Microba.init() is called in a static constructor
    UIManager.put("microba.DatePickerUI",
    "xxx.xxx.MyDatePickerUI");

     
  • Martijn van Tilburg

    Another similar solution that does not depend on JGoodies:

    /**
    * Customized DatePicker UI delegate object that sets the date value to null if
    * the textfield is empty or contains solely whitespace
    */
    public class MyDatePickerUI extends BasicDatePickerUI implements FocusListener {
    static {
    UIManager.put("microba.DatePickerUI", "MyDatePickerUI");
    }

    public static ComponentUI createUI(JComponent c) {
    return new MyDatePickerUI();
    }

    @Override
    protected void installComponents() {
    super.installComponents();
    field.addFocusListener(this);
    }

    public void focusGained(FocusEvent e) {
    // No additional behavior
    }

    public void focusLost(FocusEvent e) {
    if (((JFormattedTextField)e.getComponent()).getText().trim().equals("")) {
    ((JFormattedTextField)e.getComponent()).setValue(null);
    }
    }
    }

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks