如何在swing中使用JDatePicker选择日期?

时间:2017-01-16 16:17:19

标签: java swing nullpointerexception datepicker jcalendar

当我尝试使用JDatePicker添加日历时,不会打印所选的日历日期。

我对是否正确选择日历日期感到困惑。当我在日历中选择日期时,我收到以下错误。

  

拾起JAVA_TOOL_OPTIONS:-agentlib:jvmhook线程中的异常   " AWT-EventQueue的-0" java.lang.NullPointerException at   EDACheck.actionPerformed(StudentCheck.java:177)at   org.jdatepicker.impl.JDatePanelImpl.fireActionPerformed(JDatePanelImpl.java:129)     在   org.jdatepicker.impl.JDatePanelImpl.access $ 1800(JDatePanelImpl.java:89)     在   org.jdatepicker.impl.JDatePanelImpl $ InternalController.mousePressed(JDatePanelImpl.java:710)     在java.awt.AWTEventMulticaster.mousePressed(未知来源)at   java.awt.Component.processMouseEvent(未知来源)at   javax.swing.JComponent.processMouseEvent(未知来源)at   java.awt.Component.processEvent(未知来源)at   java.awt.Container.processEvent(未知来源)at   java.awt.Component.dispatchEventImpl(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at   java.awt.LightweightDispatcher.processMouseEvent(未知来源)at   java.awt.LightweightDispatcher.dispatchEvent(未知来源)at   java.awt.Container.dispatchEventImpl(未知来源)at   java.awt.Component.dispatchEvent(未知来源)at   java.awt.EventQueue.dispatchEventImpl(未知来源)at   java.awt.EventQueue.access $ 200(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.awt.EventQueue $ 3.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来源)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue $ 4.run(未知来源)at   java.awt.EventQueue $ 4.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知   来自)java.awt.EventQueue.dispatchEvent(未知来源)at   java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源)     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)     在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知   来自java.awt.EventDispatchThread.pumpEvents(未知来源)     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at   java.awt.EventDispatchThread.run(未知来源)

以下是我试过的代码,

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import org.jdatepicker.JDatePicker;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePanelImpl;
import org.jdatepicker.impl.JDatePickerImpl;
import org.jdatepicker.impl.UtilCalendarModel;
import org.jdatepicker.impl.UtilDateModel;

public class StudentCheck extends JPanel implements ActionListener {

    private JLabel jcomp1;
    private JTextField jcomp2;
    private JLabel jcomp3;
    private JTextField jcomp4;
    private JLabel jcomp5;
    private JTextField jcomp6;
    private JLabel jcomp7;
    private JTextField jcomp8;
    private JComboBox jcomp9;
    private JLabel jcomp10;
    private JButton jcomp11;
    private JLabel jcomp12;
    private JLabel jcomp13;
    private JTextField jcomp14;
    private JLabel jcomp15;
    private JLabel jcomp16;
    private JTextField jcomp17;
    private JTextField jcomp18;
    private JLabel jcomp19;
    private JTextField jcomp20;
    private JDatePickerImpl datePicker;

    public StudentCheck() {

        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        //tabbedPane.setBounds(10, 211, 733, 500);
        add(tabbedPane);

        JPanel panel1 = new JPanel();
        JPanel panel2 = new JPanel();

         tabbedPane.add("Student Check", panel1);

         tabbedPane.addTab("Student Check", null, panel1, null);

         tabbedPane.add("Teacher Check", panel2);

         tabbedPane.addTab("Teacher Check", null, panel2, null);
        //construct preComponents
         String[] jcomp9Items = {"maths", "science", "chemistry"};

         UtilDateModel model = new UtilDateModel();
        //model.setDate(20,04,2014);
        // Need this...
        Properties p = new Properties();
        p.put("text.today", "Today");
        p.put("text.month", "Month");
        p.put("text.year", "Year");
        JDatePanelImpl datePanel = new JDatePanelImpl(model, p);
        // Don't know about the formatter, but there it is...
        datePicker = new JDatePickerImpl(datePanel, new DateLabelFormatter());


        JButton buttonOK = new JButton("OK");


        //construct components
         jcomp1 = new JLabel ("Student Number");
         jcomp2 = new JTextField (5);
         jcomp3 = new JLabel ("Dep Date");
         //jcomp4 = new JTextField (5);
         jcomp5 = new JLabel ("  Dep Status");
         jcomp6 = new JTextField (5);
         jcomp7 = new JLabel ("          Options");
         jcomp8 = new JTextField (5);
         jcomp9 = new JComboBox (jcomp9Items);
         jcomp10 = new JLabel ("  platofrms");
         jcomp11 = new JButton ("Submit");
         jcomp12 = new JLabel ("                                                               Student and Teacher Availability Results in EDA");
         jcomp13 = new JLabel ("             blue flags");
         jcomp14 = new JTextField (5);
         jcomp15 = new JLabel ("    red flags");
         jcomp16 = new JLabel ("green flags");
         jcomp17 = new JTextField (5);
         jcomp18 = new JTextField (5);
         jcomp19 = new JLabel ("          Last  marks");
         jcomp20 = new JTextField (5);
         datePicker=new JDatePickerImpl(datePanel, null);
         panel1.add(datePicker);
         panel1.add(buttonOK);
         datePicker.addActionListener(this);

         buttonOK.addActionListener(this);


         jcomp11.addActionListener(this);
         jcomp9.addActionListener(this);

        //set components properties
        jcomp1.setToolTipText ("F");
        jcomp16.setToolTipText ("S");

        //adjust size and set layout
        panel1.setPreferredSize (new Dimension (733, 500));
        panel1.setLayout (null);

        //add components
        panel1.add (jcomp1);
        panel1.add (jcomp2);
        panel1.add (jcomp3);
        //panel1.add (jcomp4);
        panel1.add(datePicker);
        panel1.add (jcomp5);
        panel1.add (jcomp6);
        panel1.add (jcomp7);
        panel1.add (jcomp8);
        panel1.add (jcomp9);
        panel1.add (jcomp10);
        panel1.add (jcomp11);
        panel1.add (jcomp12);
        panel1.add (jcomp13);
        panel1.add (jcomp14);
        panel1.add (jcomp15);
        panel1.add (jcomp16);
        panel1.add (jcomp17);
        panel1.add (jcomp18);
        panel1.add (jcomp19);
        panel1.add (jcomp20);

        //set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds (50, 50, 100, 25);
        jcomp2.setBounds (180, 50, 100, 25);
        jcomp3.setBounds (340, 50, 100, 25);
        //jcomp4.setBounds (455, 50, 125, 25);
        jcomp5.setBounds (50, 95, 100, 25);
        jcomp6.setBounds (180, 100, 100, 25);
        jcomp7.setBounds (320, 100, 100, 25);
        jcomp8.setBounds (455, 95, 125, 25);
        jcomp9.setBounds (180, 160, 100, 25);
        jcomp10.setBounds (45, 160, 100, 25);
        jcomp11.setBounds (305, 210, 100, 25);
        jcomp12.setBounds (60, 250, 630, 25);
        jcomp13.setBounds (30, 290, 120, 25);
        jcomp14.setBounds (295, 290, 165, 25);
        jcomp15.setBounds (30, 335, 160, 25);
        jcomp16.setBounds (25, 370, 185, 30);
        jcomp17.setBounds (295, 330, 165, 25);
        jcomp18.setBounds (295, 370, 165, 25);
        jcomp19.setBounds (25, 410, 210, 25);
        jcomp20.setBounds (295, 405, 165, 25);
        datePicker.setBounds (455, 50, 125, 25);

    }
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        // for UtilDateModel, the value returned is of type java.util.Date
        Date selectedDate = (Date) datePicker.getModel().getValue();
        System.out.println(selectedDate);

        // for UtilCalendarModel, the value returned is of type java.util.Calendar
//      Calendar selectedValue = (Calendar) datePicker.getModel().getValue();
//      Date selectedDate = selectedValue.getTime();

        // for SqlDateModel, the value returned is of type java.sql.Date
//      java.sql.Date selectedDate = (java.sql.Date) datePicker.getModel().getValue();

        JOptionPane.showMessageDialog(this, "The selected date is " + selectedDate);
    //some condtions to connect the database..

    //Getting the date from the caldenar in the format as YYYY-MM-DD and pass into the sql query.
}

    public static void main (String[] args) {
        JFrame frame = new JFrame ("MyPanel");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new StudentCheck());
        frame.pack();

        frame.setVisible (true);
    }


}

相同的代码会使用以下类别调用所选日期的日期转换

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JFormattedTextField.AbstractFormatter;

public class DateLabelFormatter extends AbstractFormatter {

    private String datePattern = "yyyy-MM-dd";
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern);

    @Override
    public Object stringToValue(String text) throws ParseException {
        return dateFormatter.parseObject(text);
    }

    @Override
    public String valueToString(Object value) throws ParseException {
        if (value != null) {
            Calendar cal = (Calendar) value;
            return dateFormatter.format(cal.getTime());
        }

        return "";
    }

}

当我点击它获得上述错误的日期时,但预期的结果是应该在控制台中打印所选日期,它也应该显示在弹出窗口中。

以下jars版本我正在使用这个摇摆gui,

  • JdatePicker1.3.4.jar
  • xdb.jar
  • ojdbc.jar
  • xmlparserv2.jar

0 个答案:

没有答案