2014-03-24 4 views
0

JCalendar 간단한 자바 프로그램이 있습니다. 내 JCalendar calendario이 비 었는지 여부를 알아야합니다. 이는 사용자가 날짜를 선택했는지 여부를 의미합니다. 내가 calendario.isEmpty 같은 방법에 대해 생각하고 있었지만 그것은 존재하지 않습니다. 어쩌면 calendarionull을 비교할 수 있지만 작동하지 않습니다. com.toedter.calendar.JDateChooser을 사용 중입니다.JCalendar가 비어 있는지 확인하는 방법?

답변

4

com.toedter.calendar.JDateChooser을 사용하는 경우 가장 좋은 방법은 인스턴스 메서드 getDate()을 호출 한 반환 날짜를 확인하는 것입니다. 반환 된 날짜가 null이면 사용자가 날짜를 선택하지 않았 음을 의미합니다. 예컨대 :

public class TestCalendar extends JFrame implements ActionListener { 

    private JDateChooser dateChooser; 

    public TestCalendar() { 
    super("Simple"); 
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    setLayout(new FlowLayout()); 
    dateChooser = new JDateChooser(); 
    add(dateChooser); 
    JButton button = new JButton("Check"); 
    button.addActionListener(this); 
    add(button); 
    setSize(300, 100); 
    } 

    public void actionPerformed(ActionEvent e) { 
    Date date = dateChooser.getDate(); 
    if (date == null) { 
     JOptionPane.showMessageDialog(TestCalendar.this, "Date is required."); 
    } 
    } 

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

} 
1

이뿐만 아니라

if(jDateChooser1.getDate()==null){ 
    JOptionPane.showMessageDialog(this, "Date not selected"); 
} 
을 시도 할 수 있습니다
관련 문제