java - getTableCellRendererComponent参数: "Value" and "IsSelected" for JComboBox

标签 java swing tablecellrenderer

我在 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”怎么可能为假?如果为 false,为什么要渲染“值”,因为它没有被选择?会呈现什么? TIA。

camickr 澄清和一些实验后更新

显然我只部分了解正在发生的事情,这给我带来了一个问题。当进行 JComboBox 选择时,“value”的内容是所选项目而不是 JComboBox 实例。因此我不再需要渲染 JComboBox 的实例。我也没有看到“表”的方法可以让我获取当前单元格中的组件。如何获取 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);
}

}

最佳答案

每当您单击单元格时,选定的行就会发生变化。

因此,由于行突出显示需要更改,因此需要渲染行中的每个单元格。

在行内,一次只能选择一个单元格。

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

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

关于java - getTableCellRendererComponent参数: "Value" and "IsSelected" for JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48288260/

相关文章:

swing - 使用单元格渲染器后禁用行选择功能

java - JTable 在多个标题插入后更新列标题高度

java - JTable中如何改变行的颜色

java - 在 MacOS 上取消装饰 JInternalFrame

java - 在java中从外部文件获取值的最佳方法

java - 按下按钮播放文件时出错

java - 创建一个灵活的临时游戏数据存储系统

java - 单击之前图像呈灰色 - 更改为颜色并记住单击的图像

java - 通过选项列表显示结果的最佳方式

java - 如何使用designgridlayout在页面中的两类数据之间插入垂直JSeperator?