简体   繁体   English

使JTable中的单元格可编辑-单元格的默认值

[英]Making a cell in a JTable editable - the default value of the cell

I am working with Java, and I am trying to make a cell in JTable to be editable. 我正在使用Java,并且正在尝试使JTable中的单元格可编辑。 My class implements TableModel and extends AbstractTableModel (so that I can use the method fireTableCellUpdated(rowIndex, columnIndex)) , and I have implemented the methods isCellEditable() and setValueAt() . 我的课程实现了TableModel并扩展了AbstractTableModel(以便可以使用fireTableCellUpdated(rowIndex, columnIndex)) ,并且已经实现了isCellEditable()setValueAt() I represent a single cell in the table as an object of class Cell. 我将表格中的单个单元格表示为Cell类的对象。

Now here is my problem: the cell is already editable, and when I click on it, the cursor appears in the cell, however, there appears also a string in the cell like this: Cell@1e63e3d . 现在这是我的问题:该单元格已经可以编辑,当我单击它时,光标出现在该单元格中,但是,该单元格中还出现了一个字符串,如下所示: Cell@1e63e3d I delete this string and put in the cell the value, which I want to put, then click Enter and it works fine. 我删除此字符串,然后将要放入的值放在单元格中,然后单击Enter,它可以正常工作。 But I want when I click on the cell there to appear nothing, an empty string, and not Cell@1e63e3d . 但是我想当我单击该单元格时不显示任何内容,一个空字符串,而不显示Cell@1e63e3d And I don't know how to set this empty string as default and where. 而且我不知道如何将此空字符串设置为默认值以及在何处。

My Cell class stores information (characteristics) about the cell like color of the cell, and its value as instance variables. My Cell类存储有关单元格的信息(特征),例如单元格的颜色,以及其值作为实例变量。

Please tell me if you need more information. 如果您需要更多信息,请告诉我。

Have you set a TableCellRenderer and TableCellEditor for your JTable ? 您是否为JTable设置了TableCellRendererTableCellEditor

For displaying a cell , the TableCellRenderer is used to render the contents for a location from the TableModel . 为了显示单元格TableCellRenderer用于呈现TableModel某个位置的内容。 By default, it will use the toString method of the Object in that location, so that would explain the Cell@1e63e3d being displayed in the cell -- that is the result of the toString method being called on your Cell object. 默认情况下,它将在该位置使用ObjecttoString方法,因此可以解释在Cell@1e63e3d中显示Cell@1e63e3d原因-这是在Cell对象上调用toString方法的结果。

By writing a custom cell renderer (a class that implements TableCellRenderer ), you'll be able to return a Component you want to use to display the Cell object, using the getTableCellRendererComponent method. 通过编写自定义单元格渲染器(实现TableCellRenderer的类),您可以使用getTableCellRendererComponent方法返回要用于显示Cell对象的Component In your case, you may want to subclass a JLabel which implements TableCellRenderer and will set the contents of the label to reflect the contents of your Cell object. 在您的情况下,您可能想对实现TableCellRendererJLabel进行子类化,它将设置标签的内容以反映Cell对象的内容。

As for editing a cell , the TableCellEditor receives the Object from the TableModel when you want to edit a cell with in the JTable . 至于编辑单元格 ,当您要在JTable 编辑单元格时TableCellEditor将从TableModel接收Object The TableCellEditor will return a Component which is used to edit the cell contents (the Object ) using the getTableCellEditorComponent method. TableCellEditor将返回一个Component ,该Component用于使用getTableCellEditorComponent方法来编辑单元格内容( Object )。

In the case you provide, I think that making a JTextField which implements the TableCellEditor interface will be able to do the job for you. 在您提供的情况下,我认为制作一个实现TableCellEditor接口的JTextField将能够为您完成这项工作。 When you override the getTableCellEditorComponent , check that you have an instance of the Cell object (ie object instanceof Cell ) and if that's the case, initialize your JTextField to contain the contents of your Cell object that you want to display or edit. 重写getTableCellEditorComponent ,请检查是否具有Cell对象的实例(即object instanceof Cell ),如果是这种情况,请初始化JTextField以包含要显示或编辑的Cell对象的内容。

Recommended reading: I found the Rendering cells in Swing's JTable component article from IBM developerWorks to be quite helpful in learning how to deal with JTable s and their cell rendering and editing features. 推荐阅读:我发现IBM developerWorks的Swing的JTable组件文章中的Rendering单元对于学习如何处理JTable及其单元渲染和编辑功能非常有帮助。 In particular, the Creating custom renderers and Editiing table cells sections may be of interest. 特别是,“ 创建自定义渲染器”和“ 编辑表格单元格”部分可能会引起您的兴趣。

Are you using an appropriate TableCellEditor to display the component for editing? 您是否使用适当的TableCellEditor显示要编辑的组件?

class MyTableCellEditor
        extends DefaultCellEditor
{
    @Override
    public Component getTableCellEditorComponent(
            JTable table,
            Object value,
            boolean isSelected,
            int row,
            int column)
    {
        final JTextField c = (JTextField) super.getTableCellEditorComponent(
            table,
            ((Cell) value).text, // edit the text field of Cell
            isSelected,
            row,
            column);

        c.selectAll(); // automatically select the whole string in the cell
        return c;
    }
}

You will need to tell your table to use this custom cell editor, in addition to the custom cell renderer. 除了自定义单元格渲染器之外,您还需要告诉表使用此自定义单元格编辑器。

myTable.setDefaultEditor(Cell.class, new MyTableCellEditor());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM