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());
}
}
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 ?
– Javinha
@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.
– user28595
@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.– user28595
wanted in dd/MM/yyyy format, plus your answer already helped, now how to convert, I am looking for.
– Javinha
@Javinha ah so I understood the question mistakenly, I will edit with the correct solution.
– user28595
ok thanks again !
– Javinha