catch and set value of a Jdatechooser

Asked

Viewed 1,069 times

0

I created a JDateChooser own, so I can make more personalized changes to it later.

The problem is, I realized, if I pick a date on it, it just gives me the current date. For example, if I choose 1990 or 2020, it will return me the current date, and in a format other than "dd/MM/yyyy" that I defined. Has some way, to make it just pick up and set in the format of "dd/MM/yyyy"?

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());
    }
}

1 answer

1


What causes this error is your method getData():

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());
    }
}   

Note that you return the current date on it. If the goal is to rescue the component date, 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 method getData() be in the format of a date dd/MM/yyyy you need to convert to string and format because the type Date always returns in format "Unix Epoch"(ex.: Wed Sep 13 16:51:55 BRT 201). For this, just edit your method as below:

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

And in 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, you will burst exception.

  • I’m changing the name of the class! So I can correctly use the methods of my class, which I could change, since I already use getDate inside ?

  • 1

    @Javinha if you want to retrieve the date that was selected in the component, yes. Your method is creating a new datechooser and setting the current date, there will only return that.

  • @Javinha a doubt that arose me here, you intend to return the formatted date to display in the text field only in format dd/MM/yyyy or timestamp format(Wed Sep 13 16:51:55 BRT 2017)? If it’s the first, I think my answer might not answer, I’d need an adaptation.

  • wanted in dd/MM/yyyy format, plus your answer already helped, now how to convert, I am looking for.

  • 1

    @Javinha ah so I understood the question mistakenly, I will edit with the correct solution.

  • ok thanks again !

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.