我构建了以下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()方法,虽然我不知道如何实现它。
如何获取日期并在文本字段中以dd / MM / yyyy格式显示?
修改:
我在看到建议后尝试了以下内容:
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
String date = sdf.format(availFromDate.getDate());
availablefromT.setText(date);
虽然,现在我得到一个NullPointerException。谁知道为什么?这似乎与此有关: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);
}
}
答案 0 :(得分:2)
相同的代码在这里工作
//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);
}
答案 1 :(得分:2)
将JDateChooser强烈关注到 JCalendar.jar v.1.4 :
以这种方式获取Date选择器的“SelectedDate”(在使用语句创建名为dataEmissioneTextField
的JDataChooser对象之后):
Date date = dataEmissioneTextField.getDateEditor().getDate();
如果我们想要选择具有特定侦听器的日期来保存所选日期的值,我们可以按如下方式编写:
dataEmissioneTextField.getDateEditor().addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO Auto-generated method stub
if ("date".equals(evt.getPropertyName())) {
//Get the selected date
Date date = dataEmissioneTextField.getDateEditor().getDate();
dataConsegnaTextField.setDate(dataConsegnaTextField.getDateEditor().getDate());
ordine.setOrderDeliveryDate(date);
//Draws a Green Border when the date is selected
dataEmissioneTextField.setBorder(BorderFactory.createLineBorder(Color.GREEN));
} else {
//Draws a Red Border when the date is not selected
dataEmissioneTextField.setBorder(BorderFactory.createLineBorder(Color.RED));
}
}
});
请注意,JDateChooser
只支持PropertyChangeListener
。
Reguards。
答案 2 :(得分:1)
要使用jCalendar,您可以使用以下代码。
String jcalender=calendar.getDate().toString();
label.setText(jcalender);
答案 3 :(得分:0)
好的,你有JDateChooser和JTextField。您将需要一些东西来触发更改,因此日期将转到文本。首先,您需要创建一个对象类的新实例。执行此操作时,调用方法getData(在Web上找到它)并将其存储到对象变量中。然后创建新的变量,即字符串并在那里存储对象。最后,在JTextField中显示文本。
所以代码应如下所示:
public Object x;
public string y;
x = getData(availFromDate);
y = x.toString();
JTextField.setText(y);
如果这不起作用,请尝试:
y = string.valueOf(availFromDate);
enter code here
答案 4 :(得分:0)
你可以这样做
public void jdatechooserexample() {
JDateChooser chooser=new JDateChooser();
JTextField field=new JTextField(15);
chooser.addPropertyChangeListener("date",new PropertyChangeListener () {
public void propertyChange(PropertyChangeEvent e){
JDateChooser chooser=(JDateChooser)e.getSource();
SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
field.setText(sdf.format(chooser.getDate()));
}
});
}
答案 5 :(得分:0)
JDateChooser仅适用于Property change事件。这可用于在Jtextfield中显示值
private void jDateChooser1PropertyChange(java.beans.PropertyChangeEvent evt) {
if ("date".equals(evt.getPropertyName())) {
//Get Date
Date date = jDateChooser1.getDateEditor().getDate();
SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
//Define the format of Date
String mydate = sf.format(date);
jTextField1.setText(mydate);
} else {
jTextField1.setText(null);
}
}