get and set value of a JDateChooser

0

I've created a JDateChooser propio, so I can make more custom changes to it later.

The problem, is that I realized, that if I choose a date on it, it just moves me to the current date. For example, if I choose 1990 or 2020, it will return me the current date, and in a format other than the "dd/MM/yyyy" that I set. Is there any way, to make it just get and set in "dd/MM/yyyy" format?

My attempt:

import com.toedter.calendar.JDateChooser;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BackGr extends JFrame {

    JDC data = new JDC();
    JPanel painel = new JPanel();

    public BackGr() {
        setSize(450, 100);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        painel.add(data);
        data.setPreferredSize(new Dimension(120, 20));

        JTextField f = new JTextField();
        painel.add(f);
        f.setEditable(false);
        f.setPreferredSize(new Dimension(200, 20));

        JButton btSetar = new JButton("Clique");
        painel.add(btSetar);
        btSetar.setPreferredSize(new Dimension(70, 20));

        btSetar.addActionListener((ActionEvent e) -> {
            Date valor = data.getData();
            f.setText("" + valor);
        });
        add(painel);
    }

    public static void main(String[] args) {
        BackGr bg = new BackGr();
        bg.setVisible(true);
    }
}

class JDC extends JDateChooser {

    public JDC() {

    }

    public void setData(Object valor) {
        setDate(((Date) valor));
    }

    public Date getData() {
        JDateChooser calendario = new JDateChooser(new Date(), "dd/MM/yyyy");
        return (calendario.getDate());
    }
}
    
asked by anonymous 02.09.2017 / 21:08

1 answer

1

What causes this error is its getData() method:

class JDC extends JDateChooser {

    public JDC() {

    }

    public void setData(Object valor) {
        setDate(((Date) valor));
    }

    public Date getData() {
        JDateChooser calendario = new JDateChooser(new Date(), "dd/MM/yyyy");
        return (calendario.getDate());
    }
}   

Notice that you return the current date on it. If the goal is to retrieve the date of the component, use the native method getDate() .

Another thing, this JDC class is not following the java naming convention, it is interesting to always follow it to avoid making your code confusing.

So that the return of the getData() method is in the form of a dd/MM/yyyy date you need to convert to string and format, since type Date always returns in the format " Unix Epoch " (ex. Wed Sep 13 16:51:55 BRT 201 ). To do this, simply edit your method as below:

public String getData() {
    Date dt = this.getDate();
    return dt != null ? new SimpleDateFormat("dd/MM/yyyy").format(dt) : ""; 
}

And no action:

btSetar.addActionListener((ActionEvent e) -> {

    f.setText(data.getData());
});

Null validation is required because there is no default date when the field is created, and if you try to retrieve the date with it empty, it will pop up an exception.

    
02.09.2017 / 21:14