Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

java - Trying to color specific cell in JTable... getTableCellRendererComponent Overide

So I know this may be a duplicate question, but I've looked through many of the ones already on here and none of them seem to work for me, so I thought I would post my own and hopefully some of the other people having trouble with this will find this helpful also.

Here is my code

    table.getColumn("Name").setCellRenderer(
                new DefaultTableCellRenderer() {
                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        setText(value.toString());

                        if (row==3) 
                        {
                            setForeground(Color.RED);
                        }
                        return this;
                    }
                }
            );

Here is what is displayed in the JFrame. As you can see I am trying to to only color the text in the third row of the Column "Name" but it colors the whole row. enter image description here

Any suggestions? Thanks! Canaan

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The render is unique for column "Name". You are setting Red as foreground color when row is 3 but you dont reset it for others rows, so when painter is called it always paint red. You have to set red when row is 3 but you also have to reset the original color in other case.

EDITED: Performed version. Now original foreground color is backed up, and super is used to render like others columns.

           table.getColumn("Name").setCellRenderer(
            new DefaultTableCellRenderer() {

        Color originalColor = null;

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            DefaultTableCellRenderer renderer = (DefaultTableCellRenderer) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (originalColor == null) {
                originalColor = getForeground();
            }
            if (value == null) {
                renderer.setText("");
            } else {
                renderer.setText(value.toString());
            }

            if (row == 3) {
                renderer.setForeground(Color.RED);
            } else {
                renderer.setForeground(originalColor); // Retore original color
            }
            return renderer;
        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...