JTable中的JSpinner(Time) [英] JSpinner (Time) in JTable
问题描述
这里是我的SpinnerEditor.class洞,添加了一些评论。
代码:
public SpinnerEditor(String timeFormat){
super(new JTextField());
//要显示的默认时间(1小时)
时间日期=新时间(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();
}
});
}
//准备微调器组件并返回它。
public Component getTableCellEditorComponent(
JTable table,Object value,boolean isSelected,int row,int column
){
if(!valueSet){
spinner.setValue值);
}
SwingUtilities.invokeLater(new Runnable(){
public void run(){
textField.requestFocus();
}
});
返回旋转;
}
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;
}
返回true;
}
//返回旋转器当前值。
public Object getCellEditorValue(){
return spinner.getValue();
}
public boolean stopCellEditing(){
System.err.println(Stopping edit);
//在stopcellEditing被称为TextField被设置为错误的值(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,
无效的值,丢弃);
}
返回true;
}
你很混乱的编辑器和渲染器。编辑器是在编辑单元格时显示的小部件。当单元格不再编辑时,单元格渲染器用于绘制单元格。这一切都在本段落中解释。我真的建议你阅读它,因为它清楚地说明了如何执行表格渲染。
您应该做的是使用自定义CellRenderer作为相关列,以便它使用您的日期格式化程序。
查看本教程了解更多信息在细胞编辑器和细胞渲染器上。
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.
这篇关于JTable中的JSpinner(Time)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!