如何在加载帧时显示日期选择器?
码
private void jDateChooser2FocusGained(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
//JDateChooser2 jdc = new JDateChooser2("DD/MM/YYYY", true);
//jDateChooser2.getDateEditor().getUiComponent().requestFocusInWindow();
jDateChooser2.requestFocusInWindow();
}
源代码
public class welcome extends javax.swing.JFrame {
public static final String DATE_FORMAT_NOW = "dd/MMM/YYYY ";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
Date date = new Date();
/**
* Creates new form welcome
*/
public welcome() {
initComponents();
sdf.setLenient(false);
String dt = sdf.format(cal.getTime());
System.out.println(dt);
try
{
date = sdf.parse(dt);
System.out.println(date);
}
catch(ParseException pe)
{
System.out.println("pe: " + pe.getMessage());
Toolkit.getDefaultToolkit().beep();
}
jLabel3.setText(dt);
jTextField1.setText(dt);
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
// System.out.println("guihgio");
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("enter press key");
this code is not working//
char c = evt.getKeyChar();
if (!((c >= '0') && (c <= '9') ||
(c == KeyEvent.VK_BACK_SPACE) ||
(c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_SLASH)))
{
JOptionPane.showMessageDialog(null, "Please Enter Valid");
evt.consume();
}
}
MY EVENT CODE private void jSpinner1KeyPressed(java.awt.event.KeyEvent evt){
// TODO在这里添加您的处理代码:
的System.out.println(&#34; spiinejngyjgkur&#34);
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
//password pwd = new password();
//pwd.setVisible(true);
jSpinner1.requestFocus();
if (evt.getSource() == jSpinner1) //add
{
try {
String host = "jdbc:mysql://localhost:3306/indospirit";
String uName = "root";
String uPass = "paras123";
//Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Driver d = new com.mysql.jdbc.Driver();
Connection con = DriverManager.getConnection(host, uName, uPass);
PreparedStatement ps;
System.out.println("weww");
ps = con.prepareStatement("INSERT INTO `log1`(`date`)VALUES('" + dateString + "')");
int i = ps.executeUpdate();
if (i > 0) {
JOptionPane.showMessageDialog(null, "Record Added");
} else {
JOptionPane.showMessageDialog(null, "Record NOT Added");
}
} catch (SQLException ex) {
//ex.printStackTrace();
System.err.println(ex.toString());
} catch (Exception ex1) {
//ex1.printStackTrace();
System.err.println(ex1.toString());
}
}
try {
String host = "jdbc:mysql://localhost:3306/indospirit";
String uName = "root";
String uPass = "paras123";
//Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Driver d = new com.mysql.jdbc.Driver();
Connection con = DriverManager.getConnection(host, uName, uPass);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * from `log1` ORDER BY `date` DESC limit 1");
while (rs.next()) {
int col = rs.getInt("id");
String str = rs.getString("date");
jLabel10.setText(str);
System.out.println(col + " " + str + " ");
}
} catch (SQLException ex) {
System.err.println(ex.toString());
} catch (Exception ex1) {
System.err.println(ex1.toString());
}
}
event not working
private void jSpinner1StateChanged(javax.swing.event.ChangeEvent evt){
// TODO在这里添加您的处理代码:
的System.out.println(&#34; gijjhbip&#34);
System.out.println(&#34;来源:&#34; + evt.getSource());
}
how could I check THAT date Enterd by user in date format in JFormattedTextField...please help IN KEY PRESSED EVENT
my code is here
private void jFormattedTextField1KeyPressed(java.awt.event.KeyEvent evt){
// TODO在这里添加您的处理代码:
System.out.println(evt.getKeyChar());
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
Date date = (Date) evt.getSource();
if(!(date.equals(format)))
{
evt.consume();
}
}
答案 0 :(得分:1)
“如何验证文本字段,用户只能输入日期格式和”
恕我直言,获取日期的用户输入是个坏主意。验证输入将变得令人头疼并且对于程序来说效率低,因为用户可以输入任何字符组合。相反,您可以使用JSpinner
DateListModel
请参阅Spinner tutorial | SpinnerDateModel javadoc
但话又说回来,为什么甚至允许今天的日期输入?除了从用户那里获得输入之外,可以通过多种方式检索该数据。
如果你真的坚持按照自己的方式进行操作,输入文本字段并想要验证,你总是可以抓住ParseException
。但在这种情况下,您需要向用户指定需要输入的确切格式,并在用户输入格式不正确或不是真实日期时提供某种通知。
SimpleDateFormat formatter = new SimplDateFormat(DATE_FORMAT_NOW);
String dateString = textField.getText();
Date date;
try {
date = formatter.parse(dateString);
} catch (ParseException) {
statusLabel.setText("Please enter a valid date");
// or JOptionPane.showMessageDialog(null, "Please Enter a valid Date");
}
但问题是用户可以输入一个随机日期,例如2102中的某个日期。所以我建议使用JSpinner
或不可编辑的文本字段,只显示今天的日期。
更新试试这个
import java.util.Date; import java.text.SimpleDateFormat; import javax.swing。*;
public class SpinnerDateTest {
public static void main(String[] args) {
Date date = new Date();
JSpinner spinner = new JSpinner();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM//yyyy");
String dateString = formatter.format(date);
spinner.setModel(new SpinnerListModel(new String[]{dateString}));
JOptionPane.showConfirmDialog(null, spinner, "Spinner:"
,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
}
或使用SpinnerDateModel
public static void main(String[] args) {
Date date = new Date();
JSpinner spinner = new JSpinner();
spinner.setModel(new SpinnerDateModel(date, null, null, Calendar.DAY_OF_WEEK));
JOptionPane.showConfirmDialog(null, spinner, "Spinner:"
,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
答案 1 :(得分:0)
我不知道您使用的是哪个库,但为什么不试试这个jcalendar。
第一个问题: 你可以使用JForxtField,JTextField的instand。
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
DateFormatter formatter = new DateFormatter(format);
format.setLenient(false);
formatter.setAllowsInvalid(false);
formatter.setOverwriteMode(true);
JFormattedTextField formattedTextField = new JFormattedTextField(formatter);
formattedTextField.setValue(new Date());
formattedTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent evt) {
//System.out.println(evt.getKeyChar());
//TODO
}
});
第二个问题: getFocuse的唯一方法是更改JDateChoose的源代码; 因为源代码是
protected JButton calendarButton;
...
calendarButton = new JButton(icon) {
public boolean isFocusable() {
return false;
}
};
答案 2 :(得分:0)
您可以将其用于焦点
jDateChooser.getDateEditor().getUiComponent().requestFocusInWindow();
如果你想控制一些事件
jDateChooser.getDateEditor().getUiComponent().addKeyListener(new listenerClass());