I am trying to implement an JSpinner for time in to a JTable , it worked preaty goot at the first look but after losing the focus of the cell the edited cell is being set to “Thu Jan 01 +time+ UTC 1970” the time is being set correctly. How can I remove the Date from the Time ?
here is my hole SpinnerEditor.class , have added some comments.
Code :
public SpinnerEditor(String timeFormat) {
super(new JTextField());
// Default Time I want to Display (1 Hour)
Time date = new Time(3600000);
SpinnerDateModel timeModel = new SpinnerDateModel(date, null, null,Calendar.MINUTE);
spinner = new JSpinner(timeModel);
editorDate = new JSpinner.DateEditor(spinner, timeFormat);
spinner.setEditor(editorDate);
editorDate = ((JSpinner.DateEditor)spinner.getEditor());
// println result : "Thu Jan 01 01:00:00 UTC 1970"
System.out.println(editorDate.getTextField().getValue());
textField = editorDate.getTextField();
textField.addFocusListener( new FocusListener() {
public void focusGained( FocusEvent fe ) {
System.err.println("Got focus");
//textField.setSelectionStart(0);
//textField.setSelectionEnd(1);
SwingUtilities.invokeLater( new Runnable() {
public void run() {
if ( valueSet ) {
textField.setCaretPosition(1);
}
}
});
}
public void focusLost( FocusEvent fe ) {
}
});
textField.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae ) {
stopCellEditing();
}
});
}
// Prepares the spinner component and returns it.
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column
) {
if ( !valueSet ) {
spinner.setValue(value);
}
SwingUtilities.invokeLater( new Runnable() {
public void run() {
textField.requestFocus();
}
});
return spinner;
}
public boolean isCellEditable( EventObject eo ) {
System.err.println("isCellEditable");
if ( eo instanceof KeyEvent ) {
KeyEvent ke = (KeyEvent)eo;
System.err.println("key event: "+ke.getKeyChar());
textField.setText(String.valueOf(ke.getKeyChar()));
valueSet = true;
} else {
valueSet = false;
}
return true;
}
// Returns the spinners current value.
public Object getCellEditorValue() {
return spinner.getValue();
}
public boolean stopCellEditing() {
System.err.println("Stopping edit");
// after stopcellEditing is called the TextField is being set with the wrong values (Thu Jan 01 01:00:00 UTC 1970)
super.stopCellEditing();
try {
if( editorNumeric!=null)
{
editorNumeric.commitEdit();
spinner.commitEdit();
}
if( editorDate!=null)
{
SimpleDateFormat lFormat = new SimpleDateFormat("HH:mm");
textField.setText((spinner.getValue() != null) ? lFormat.format(spinner.getValue()) : "");
}
} catch ( java.text.ParseException e ) {
JOptionPane.showMessageDialog(null,
"Invalid value, discarding.");
}
return true;
}
You are confusing Editors and Renderers. Editor is a widget displayed when a cell is being edited. When the cell is no longer edited, the cell renderer is used to “paint” the cell. This is all explained in this paragraph. I would really recommend you to read it, as it explains clearly how table rendering is performed.
What you should do, is use a Custom CellRenderer for the involved column so that it uses your date formatter.
Take a look at this tutorial for more information on cell editors and cell renderers.