当前位置: 首页 > 面试题库 >

JTable中的JSpinner(时间)

百里弘致
2023-03-14
问题内容

我试图在JTable中实现一个JSpinner的时间,乍一看它工作得很好,但是在失去单元格的焦点之后,已编辑的单元格被设置为“ Thu Jan 01 +
time + UTC 1970”正确设置。如何从时间中删除日期?

这是我的孔SpinnerEditor.class,添加了一些注释。

代码:

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;
}

问题答案:

您会混淆编辑器和渲染器。编辑器是在编辑单元格时显示的小部件。当不再编辑单元格时,将使用单元格渲染器“绘制”该单元格。这一切都在本段中说明。我真的建议您阅读它,因为它清楚地说明了表呈现的执行方式。

您应该做的是对相关列使用自定义CellRenderer,以便它使用日期格式程序。

请看一下本教程,以获取有关单元格编辑器和单元格渲染器的更多信息。



 类似资料:
  • 介绍 (Introduction) JSpinner类是一个组件,它允许用户使用输入字段从有序序列中选择数字或对象值。 Class 声明 (Class Declaration) 以下是javax.swing.JSpinner类的声明 - public class JSpinner extends JComponent implements Accessible 类构造函数 (C

  • 问题内容: 我正在使用JSpinner输入时间;但是,当我使用getValue方法时,我获得的时间是1970年1月1日,因为这是默认的开始日期。我该如何获取时间,以及独自一人的时间?我对日期不感兴趣 注意:我已经使用了dateEditor。也许我的JSpinnerDateModel不适当? 问题答案: 您可以创建一个JSpinner实例并将其格式化为时间格式,然后在末尾提取时间:

  • 主要内容:1 Java JSpinner的介绍,2 Java JSpinner的声明,3 Java JSpinner的构造方法,4 Java JSpinner的方法,5 Java JSpinner的案例1,6 Java JSpinner的案例21 Java JSpinner的介绍 JSpinner类的对象是单行输入字段,允许用户从有序序列中选择数字或对象值。 2 Java JSpinner的声明 让我们看看javax.swing.JSpinner类的声明。 3 Java JSpinner的构造方

  • 问题内容: 我想更改JSpinner的行为,以便在单击文本时将其选中。这样可以更轻松地将字段替换为所需的值。不幸的是,我无法使该行为起作用,它只是将光标插入文本中,而没有选择已经存在的内容。 我曾尝试通过既向JSpinner本身又向文本区域本身添加了一个焦点侦听器,但这些似乎都没有达到预期的效果。我的代码(针对JSpinner本身)如下: 我不确定是什么问题。如果有关系,我正在运行Mac OS 1

  • 我已经建立了一个,并使其包含18列。最初,当它是13列时,它非常适合JTable,并且没有撕裂。然而,一旦我使它包含18个表,并将自动调整大小设置为关闭,当我使用水平滚动条尝试滚动其他列时,JTable的撕裂开始发生,如图所示: Netbeans还开始向我发送大量错误消息,如: 为什么会这样?这个问题有什么解决办法吗?谢谢 真抱歉!下面是用于初始化表的代码。希望会有用!

  • 我试图传入一个JTable对象,它有DiscountID和Name列。我基本上是在尝试获取JTable a(折扣ID)中选择的第一列的一行,以及在循环中与JTable B中选择的记录相关的这组数据。 我现在的问题是,因为最初我必须将其设置为0,以避免出现但当我运行程序时,JTable B将显示与JTable A中的第一条记录相关的信息,而不需要我选择任何数据。我不想在JTTable B中显示任何信