为什么我的 Java 自定义单元格渲染器在选择行/单元格时不显示突出显示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5557485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:39:31  来源:igfitidea点击:

Why does my Java custom cell renderer not show highlighting when the row/cell is selected?

javajtabletablecellrenderer

提问by Brian T Hannan

I have a custom cell renderer for a cell to do a word wrap so more content can be read. Here is the code:

我有一个用于单元格的自定义单元格渲染器来进行自动换行,以便可以读取更多内容。这是代码:

import java.awt.Color;
import java.awt.Component;
import java.awt.Insets;

import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;

public class TextWrapCellRenderer extends JTextArea implements TableCellRenderer {
    private static final long serialVersionUID = 1L;

    public TextWrapCellRenderer() {
        setLineWrap(true);
        setWrapStyleWord(true);
        setMargin(new Insets(0, 5, 0, 5));
        setSelectionColor(Color.GREEN);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setText((String)value);
        setSize(table.getColumnModel().getColumn(column).getWidth(),getPreferredSize().height);
        setSelectionColor(Color.GREEN);

        return this;
    }
}

Update: The cell renderer is used properly but when the user selects a row in the JTable, then it only shows the highlighting for the non-custom rendered cells. The highlighting shows for all other cells for that row though. This leaves just one cell with a white background while the rest of the row has blue (in my case) as the highlighted background color.

更新:单元格渲染器使用正确,但是当用户在 JTable 中选择一行时,它只显示非自定义渲染单元格的突出显示。但是,该行的所有其他单元格的突出显示显示。这仅留下一个具有白色背景的单元格,而该行的其余部分以蓝色(在我的情况下)作为突出显示的背景颜色。

回答by Uhlen

You have to check the isSelectedargument to see if the cell is selected or not, something like:

您必须检查isSelected参数以查看单元格是否被选中,例如:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
{
        setText((String)value);
        setSize(table.getColumnModel().getColumn(column).getWidth(),getPreferredSize().height);
        setSelectionColor(Color.GREEN);

        if (isSelected)
        {
            setBackground(table.getSelectionBackground());
            setForeground(table.getSelectionForeground());
        }
        else
        {
            setBackground(table.getBackground());
            setForeground(table.getForeground());
        }
        return this;
    }

回答by Goblin Alchemist

I think you should call the default implementation first:

我认为您应该先调用默认实现:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
    ...

The default implementation will process all usual arguments such as isSelectedand hasFocus, set the text and background color, activate the focus border etc. Then you will change the displayed text, change the cell size and return this.

默认实现将处理所有常用参数,例如isSelectedand hasFocus、设置文本和背景颜色、激活焦点边框等。然后您将更改显示的文本、更改单元格大小和return this

回答by Marcos Vasconcelos

Using setSelectionColor(Color.GREEN); you are telling just what user select is green. What's is your problem and what you expect your code to do?

使用 setSelectionColor(Color.GREEN); 你只是告诉用户选择的是绿色的。您的问题是什么以及您希望您的代码做什么?