从JDateChooser获取日期并将其显示在JTextField中 [英] Obtaining date from JDateChooser and displaying it in JTextField

查看:320
本文介绍了从JDateChooser获取日期并将其显示在JTextField中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了以下JDateChooser:

I have constructed the following JDateChooser:

availFromDate = new JDateChooser();
availFromDate.setDateFormatString("dd/MM/yyyy");
JTextFieldDateEditor dateEditor = (JTextFieldDateEditor)availFromDate.getComponent(1);
dateEditor.setHorizontalAlignment(JTextField.RIGHT);
availFromDate.setSize(new Dimension(50, 0));
availFromDate.add(availablefromT);
calendarP.add(availFromDate);
contentPane.add(calendarP);
frame1.add(contentPane);
frame1.setVisible(true);

但是,我需要从JDateChooser中选择的日期出现在要保留的JTextField中.我知道必须涉及一个getDate()方法,尽管我不确定如何实现它.

However, I need the date selected from the JDateChooser to appear in the JTextField it is being held in. I realise there must be a getDate() method involved, though I am not sure how to implement it.

如何获取日期并以dd/MM/yyyy格式显示在文本字段中?

How do I obtain the date and display it within the textfield in the format of dd/MM/yyyy?

看到建议后,我尝试了以下方法:

I have tried the following after seeing suggestions:

  SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
  String date = sdf.format(availFromDate.getDate());
  availablefromT.setText(date);

但是,现在我收到了NullPointerException.有人知道为什么吗?似乎与此有关:String date = sdf.format(availFromDate.getDate());

Though, now I am getting a NullPointerException. Anyone know why? It seems to concern this: String date = sdf.format(availFromDate.getDate());

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1770)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:943)
    at java.text.SimpleDateFormat.format(SimpleDateFormat.java:936)
    at java.text.DateFormat.format(DateFormat.java:345)
    at Controller.makeCustEnquiryGUI(Controller.java:2061)

示例:

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import javax.swing.*;
import java.util.*;

public class CalendarTest {

    private JFrame chooseCruiseFrame;
    private JDateChooser availFromDate;
    private JTextField availablefromT;
    private JPanel contentPane;
    private JPanel centerP;

    public static void main(String[] args) {
        new CalendarTest();
    }

    public CalendarTest() {

        //////////// Creating Frame
        chooseCruiseFrame = new JFrame("");
        chooseCruiseFrame.setSize(300, 200);
        chooseCruiseFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        chooseCruiseFrame.setVisible(true);

        //////////// Creating contentPane
        contentPane = new JPanel(new GridLayout(0, 1));
        contentPane.setBackground(new java.awt.Color(255, 255, 255));
        chooseCruiseFrame.add(contentPane);
        chooseCruiseFrame.setVisible(true);

        //////////// Creating CenterP
        centerP = new JPanel();
        centerP.setBackground(new java.awt.Color(255, 255, 255));
        contentPane.add(centerP);
        chooseCruiseFrame.add(contentPane);
        chooseCruiseFrame.setVisible(true);

        // Available From Calendar
        JLabel availF = new JLabel("Available From:");
        centerP.add(availF);
        contentPane.add(centerP);
        availablefromT = new JTextField(11);
        centerP.add(availablefromT);
        contentPane.add(centerP);
        chooseCruiseFrame.add(contentPane);
        chooseCruiseFrame.setVisible(true);

        availFromDate = new JDateChooser();
        JTextFieldDateEditor dateEditor = (JTextFieldDateEditor) availFromDate.getComponent(1);
        dateEditor.setHorizontalAlignment(JTextField.RIGHT);
        availFromDate.add(availablefromT);
        centerP.add(availFromDate);
        contentPane.add(centerP);
        chooseCruiseFrame.add(contentPane);
        chooseCruiseFrame.setVisible(true);

        // Converting Date to String
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String date = sdf.format(availFromDate.getDate());
        availablefromT.setText(date);
    }
}

推荐答案

相同的代码在这里起作用

the same code works here

   //button    

        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
        String date = fmt.format(this.txt_data_ini.getDate()); //jdatechooser
        this.teste.setText(date);

    }  

这篇关于从JDateChooser获取日期并将其显示在JTextField中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆