I wrote a class to be used for representing dates as a JFormattedTextField. The problem I'm having is that when an invalid date is entered immediately the date is rolled over by the number of days/months specified. Example...January 2007/01/32 will become 2007/02/01.
Below is the code for the date class and a tester class. If anyone can tell me how to get past this behaviour it would be greatly appreciated
Thanks
Below is the code for the date class and a tester class. If anyone can tell me how to get past this behaviour it would be greatly appreciated
Thanks
Code:
/**
*
*/
package ca.gc.cra.bscs.csc.client.swing;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Date;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import ca.gc.cra.bscs.csc.common.valueobject.BusinessNumber;
import ca.gc.cra.bscs.csc.common.valueobject.SocialInsuranceNumber;
/**
* @author aberg
*
*/
public class DateTester extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel mainPanel = null;
private JLabel dateLabel = null;
private JFormattedTextField dateTextField = null;
private JLabel noDateLabel = null;
private JTextField noDateTextField = null;
/**
* This method initializes mainPanel
*
* @return javax.swing.JPanel
*/
private JPanel getMainPanel() {
if (mainPanel == null) {
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.fill = GridBagConstraints.BOTH;
gridBagConstraints3.gridy = 1;
gridBagConstraints3.weightx = 1.0;
gridBagConstraints3.insets = new Insets(10, 10, 10, 10);
gridBagConstraints3.anchor = GridBagConstraints.WEST;
gridBagConstraints3.gridx = 1;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 0;
gridBagConstraints2.insets = new Insets(10, 10, 10, 10);
gridBagConstraints2.anchor = GridBagConstraints.EAST;
gridBagConstraints2.fill = GridBagConstraints.NONE;
gridBagConstraints2.gridy = 1;
noDateLabel = new JLabel();
noDateLabel.setText("No Date");
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.BOTH;
gridBagConstraints1.gridy = 0;
gridBagConstraints1.ipadx = 0;
gridBagConstraints1.ipady = 0;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.insets = new Insets(10, 10, 10, 10);
gridBagConstraints1.anchor = GridBagConstraints.WEST;
gridBagConstraints1.gridx = 1;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.ipadx = 0;
gridBagConstraints.ipady = 0;
gridBagConstraints.insets = new Insets(10, 10, 10, 10);
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.gridwidth = 1;
gridBagConstraints.gridheight = 1;
gridBagConstraints.gridy = 0;
dateLabel = new JLabel();
dateLabel.setText("Date");
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
mainPanel.add(dateLabel, gridBagConstraints);
mainPanel.add(getDateTextField(), gridBagConstraints1);
mainPanel.add(noDateLabel, gridBagConstraints2);
mainPanel.add(getNoDateTextField(), gridBagConstraints3);
}
return mainPanel;
}
/**
* This method initializes dateTextField
*
* @return javax.swing.JTextField
*/
private JFormattedTextField getDateTextField() {
if (dateTextField == null) {
dateTextField = new DateTextField(new Date());
}
return dateTextField;
}
/**
* This method initializes noDateTextField
*
* @return javax.swing.JTextField
*/
private JTextField getNoDateTextField() {
if (noDateTextField == null) {
// noDateTextField = new JTextField();
noDateTextField = new DateTextField();
}
return noDateTextField;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
DateTester thisClass = new DateTester();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public DateTester() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(396, 322);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getMainPanel(), BorderLayout.NORTH);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="10,10"
Code:
/**
*
*/
package ca.gc.cra.bscs.csc.client.swing;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import javax.swing.JFormattedTextField;
import javax.swing.text.DateFormatter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
//import org.apache.commons.logging.Log;
import ca.gc.cra.bscs.csc.common.exception.EnvironmentException;
import ca.gc.cra.bscs.csc.common.valueobject.AccountNumber;
import ca.gc.cra.bscs.csc.common.valueobject.BusinessNumber;
import ca.gc.cra.bscs.csc.common.valueobject.SocialInsuranceNumber;
import ca.gc.cra.bscs.csc.desktop.client.util.LogHelper;
/**
* @author aberg
*
*/
public class DateTextField extends JFormattedTextField {
private static final long serialVersionUID = 1L;
//private static Log log = LogHelper.getLog(DateTextField.class);
public DateTextField() {
setDate(null);
}
public DateTextField(Date date) {
setDate(date);
}
public void setDate(Date date) {
setDateFormater();
super.setValue(date);
}
public Date getDate() {
return (Date)getValue();
}
public void setValue(Object value) {
System.out.print("Call override method: " + value);
super.setValue(value);
}
/*
* Create and set the formatter for dates.
*/
private void setDateFormater() {
DateFormatter dateDefaultMask = null;
DateFormatter dateDisplayMask = null;
MaskFormatter dateEditMask = null;
try {
dateDefaultMask = new DateFormatter(new SimpleDateFormat("yyyy-MM-dd"));
dateDisplayMask = new DateFormatter(new SimpleDateFormat("yyyy-MM-dd"));
dateEditMask = new MaskFormatter("####-##-##"){
private static final long serialVersionUID = 1L;
public Object stringToValue(String value) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("StringToValue - start: " + value + " Length: " + value.length());
// log.debug("StringToValue: " + value + " Length: " + value.length());
if (value == null || value.trim().length() == 0 ||
value.equals(" - - ")) return null;
try {
System.out.println("StringToValue - return: " + value);
return df.parse(value);
} catch(Exception e) {
//Runtime parse exception
// log.error("Can not retrieve the client identifier for the edit text: '" + value + "'", e);
e.printStackTrace();
throw new ParseException("Error", 1);
}
}
public String valueToString(Object value) throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("valueToString - start: " + (value == null ? null : df.format(value)));
if (value == null || value.equals(" - - ")) return super.valueToString(null);
Date date = (Date)value;
System.out.println("valueToString - return: " + df.format(value));
return df.format(date);
}
};
// dateEditMask.setPlaceholderCharacter('0');
// dateEditMask.setAllowsInvalid(false);
// dateEditMask.setValueContainsLiteralCharacters(false);
// dateEditMask.setOverwriteMode(true);
// dateEditMask.setCommitsOnValidEdit(false);
} catch (ParseException e) {
throw new EnvironmentException(e.getMessage(), e);
}
dateDefaultMask.setAllowsInvalid(false);
dateDefaultMask.setOverwriteMode(true);
// dateDefaultMask.setCommitsOnValidEdit(true);
setFormatterFactory(new DefaultFormatterFactory(dateDefaultMask, dateDefaultMask,
dateDefaultMask));
}
}