5
How to perform input conversion via Swing’s Jtextfield via JDBC ?
public class DadosPessoais {
private Calendar dataNascimento;
public Calendar getDataNascimento() {
return dataNascimento;
}
public void setDataNascimento(Calendar dataNascimento) {
this.dataNascimento = dataNascimento;
}
}
Screen class:
public class DadosPessoaisForm extends JFrame{
private JLabel jlDataNascimento;
//private JTextField jtfDataNascimento;
private JButton jbSave,jbUpdate;
private JFormattedTextField jftfDataNascimento;
private MaskFormatter mfDataNascimento;
try {
mfDataNascimento = new MaskFormatter("##/##/####");
mfDataNascimento.setPlaceholderCharacter('_');
} catch (ParseException e) {
e.printStackTrace();
}
jftfDataNascimento = new JFormattedTextField(mfDataNascimento);
//
jbSave = new JButton("Salvar");
jbUpdate = new JButton("Editar");
jbSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
onSaveDadosPessoais();
}
});
jbUpdate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
onUpdateDadosPessoais();
}
});
private void onSaveDadosPessoais(){
DadosPessoais dadosPessoais = new DadosPessoais();
//
dadosPessoais.setDataNascimento(Calendar.getInstance());
//
}
private void onUpdateDadosPessoais() {
DadosPessoais dadosPessoais = new DadosPessoais();
//
jtfDataNascimento.setText(dadosPessoais.getDataNascimento());
//
}
}
DAO class:
public class DadosPessoaisDAO{
public int save(DadosPessoais dadosPessoais){
//
stmt.setDate(5, (new Date(pessoaDadosPessoais.getDataNascimento().getTimeInMillis())));
//
}
}
How is the jtextfield data coming? What format? Where should the conversion be done? You left a lot of details missing from the question. See the duplicate, maybe she’ll answer you.
– user28595
I am not formatting, I am not using Jformattedtextfield or Maskformat. As it is in the code, I am not performing any conversion, the way you are saving the system time because it is a field that accepts null.
– Renoir
By the code shown, you are passing a direct Legend instance to the text field. Add a print of this field to the question, or add a [mcve], otherwise it is difficult to help.
– user28595
I just finished editing. If the field is enabled or disabled, the user entering values or not, as is the case in the 2016-06-11 format in the database.
– Renoir
I understand, but what problem faced?
– user28595
I want the user to type the dataDeNascimento in the form dd/MM/yyy format for the DAO. "Sorry for the inexperience or poor formulation of the questions and expressions".
– Renoir
But for this, you will need to use maskformatter, so the bars are placed automatically. Or you just want to validate the date he typed?
– user28595
the maskformatter is enough.
– Renoir
You can change jtextfield to jformatterfield?
– user28595
but how I perform the conversion in DAO ?
– Renoir
Well, now I understand less your problem, the field will no longer be filled? Why this line here
jtfDataNascimento.setText(dadosPessoais.getDataNascimento());
if the user is going to type?– user28595
managed in Form : Try { mfDataNascimento = new Maskformatter("#/##/##/#######"); mfDataNascimento.setPlaceholderCharacter('_'); } catch (Parseexception e) { e. printStackTrace(); } jftfDataNascimento = new Jformattedtextfield(mfDataNascimento); now I need conversion on DAO
– Renoir