getTableCellRendererComponent 参数:JComboBox 的 "Value" 和 "IsSelected"

getTableCellRendererComponent Parameters: "Value" and "IsSelected" for JComboBox

我在 JTable 中有一个 JComboBox,正在查看解释参数的 getTableCellRendererComponent 文档。

table - the JTable that is asking the renderer to draw; can be null
value - the value of the cell to be rendered. It is up to the specific renderer to interpret and draw the value. For example, if value is the string "true", it could be rendered as a string or it could be rendered as a check box that is checked. null is a valid value
isSelected - true if the cell is to be rendered with the selection highlighted; otherwise false
hasFocus - if true, render cell appropriately. For example, put a special border on the cell, if the cell can be edited, render in the color used to indicate editing
row - the row index of the cell being drawn. When drawing the header, the value of row is -1
column - the column index of the cell being drawn

我的困惑是"value"和"isSelected"。如果要呈现 "value","isSelected" 怎么可能是假的?如果为假,为什么 "value" 未被选中而被渲染?会渲染什么? TIA.

经过 camickr 的澄清和一些实验后更新

显然我只是部分理解正在发生的事情,它给我带来了一个问题。进行 JComboBox 选择时,"value" 的内容是所选项目而不是 JComboBox 实例。因此,我不再需要呈现 JComboBox 的实例。我也没有看到 "table" 的方法可以让我获取当前单元格中的组件。如何获取 JComboBox 实例以便在该单元格中正确呈现框?正如在进行选择时一样,JComboBox 消失了,我得到了案例 2、5、6、7 的运行时错误,这是有道理的,因为值现在是一个字符串而不是 JComboBox 实例。 TIA.

public class TimelineCellRenderer implements TableCellRenderer {

@SuppressWarnings("unchecked")
@Override
public Component getTableCellRendererComponent(JTable table_, Object value_, boolean isSelected_, boolean hasFocus_, int row_,int column_) {

    Component field=null;
    String str="";
    if (value_!=null) {
        str=value_.toString();
    }
    switch (column_) {
        case 0:
        case 3:
        case 4:
        case 8:
            field=new JTextField();
            ((JTextField) field).setText(str);
            break;
        case 1:
            field=new JTextField();
            ((JTextField) field).setText(Double.toString((Double) value_));
            break;
        case 2:
        case 5:
        case 6:
        case 7:
            field=(JComboBox<String>) value_;
            break;
        case 9:
            field=new JTextField();
            ((JTextField) field).setText("Add button");
            break;
        case 10:
            field=new JTextField();
            ((JTextField) field).setText("del button");
            break;
    }
    if (field instanceof JTextField) {
        Font f=field.getFont().deriveFont(Font.PLAIN, (float) 14);
        field.setFont(f);
    }
    return(field);
}

}

只要您点击一个单元格,所选行就会发生变化。

所以行中的每个单元格都需要渲染,因为行突出显示需要更改。

在该行中,任何时候只能选择一个单元格。

另外,之前选择的行的所有单元格都需要重新绘制而不突出显示。

所以基本的答案是多次调用该方法,每个单元格调用一次,参数会有所不同。