Validate Jdatechooser

Asked

Viewed 667 times

1

I am having trouble determining if the user has filled in a date field of type JDateChooser, would like to do a validation, to know if this empty or not. I tried to do by looking at the documentation of JDateChooser, didn’t work out so well, the only thing I thought about was trying to take the date with a getDate() and see if it equals an empty "mask".

In practice, I saw that it is not correct, the IDE already leaves "clear that they are incompatible types"(getDate() and equals()) !

Follow the code I tried:

package calendar;

import com.toedter.calendar.JDateChooser;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Jcalendar extends JFrame  implements ActionListener
{
    private final JDateChooser data = new JDateChooser();   
    private final JButton botao01 = new JButton("Salvar");
    public JPanel jpBotoes = new JPanel();

    public Jcalendar()
    {         
        setSize(500, 300);
        add(posicaoComponentes());
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);        
    }

    public JComponent posicaoComponentes()
    {
        JPanel jp = new JPanel();         
        jp.add(data);     
        data.setPreferredSize(new Dimension(100, 20));
        getContentPane().add("North", data);
        getContentPane().add("South", jpBotoes);        
        jpBotoes.setLayout(new GridLayout(1, 1));      
        adicionaBotao(botao01);           
        return jp;        
    }

    private void adicionaBotao(JButton botao) 
    {
        jpBotoes.add(botao);
        botao.addActionListener(this);
    }

    public boolean vazio() 
    {        
        if(data.getDate().equals(" /  /   "))
        {
            return true;
        }

        else
        {
            return false;
        }        
    }

    @Override
    public void actionPerformed(ActionEvent ae)
    {   
        if (ae.getSource() == botao01) 
        {      
            if( vazio() == true)
            {
                JOptionPane.showMessageDialog(null, "Inválido !");
            } 

            else
            {
                JOptionPane.showMessageDialog(null, "Salvo !");
            }                
        }
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(() -> 
        { 
            Jcalendar c = new Jcalendar();
            c.setVisible(true);
        });
    }        
}

1 answer

3


Try to validate by checking if the return is null:

public boolean vazio() 
{        
    return data.getDate() == null;    
}

As the source code of the component:

Returns the date. If the Jdatechooser is Started with a null date and no date was set by the user, null is returned.

That is, the method getDate() returns null if no date is given by the user, or if the field is started as null.

Obs.: I just simplified the method, because since it returns a boolean, there is no need to assemble if...else, only directly return the result of the condition.

Browser other questions tagged

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