javaeclipsejcalendar

jDateChooser - Never updates with selection


I'm trying to pull in a date so I can work with it in SQL. I put the jDateChooser on the screen and use it to define a string. Because it starts out with no value (null), it throws Null Pointer Exception. I initialize the JDateChooser with compdate.setCalendar(Calendar.getInstance()); This sets the date to today and that is the value I get returned when I pull in the string. The code is below and I would love to resolve this. I'm guessing it is from my ignorance about when the update should be firing...

    JDateChooser compdate = new JDateChooser();
    compdate.setDateFormatString("yyyy/MM/dd");
    compdate.setBounds(26, 75, 144, 23);
    compdate.setCalendar(Calendar.getInstance());
    String jcalval = (new java.text.SimpleDateFormat("yyyy/MM/dd")).format(compdate.getDate());
    panelReporting.add(compdate);
    System.out.println(jcalval);

Solution

  • The OP reported that he fixed the issue by adding a listener as suggested in the comments and the linked question:

        compdate.getDateEditor().addPropertyChangeListener(new PropertyChangeListener() {
    
            @Override
            public void propertyChange(PropertyChangeEvent e) {
    
                 if ("date".equals(e.getPropertyName())) 
                    {
                        System.out.println(e.getPropertyName()
                            + ": " + (Date) e.getNewValue());
                    }
                }
            });        
    

    (This was posted in the question, where it doesn’t belong; I just took it out into an answer.)