10

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.

Brian T Hannan
  • 3,925
  • 18
  • 56
  • 96
  • Looks like I might be able to use JTable's setSelectionBackground() or grab the selection background using getSelectionBackground() and set the selected cell background in the custom cell renderer code. – Brian T Hannan Apr 05 '11 at 20:25

3 Answers3

13

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

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;
    }
Uhlen
  • 1,778
  • 15
  • 29
  • Well if the row is selected does that mean that the cell is selected? – Brian T Hannan Apr 05 '11 at 22:04
  • @Brian The `getTableCellRendererComponent(..., int row, int column)` will handle each cell of a row. So, if a row is selected, all cells under this row should be in selected row mode. You need `isSelected()` to check whether each cell is selected or not since there are many cells in a table. – eee Apr 05 '11 at 22:58
  • Perfect! Makes sense and a copy-n-paste in 3 seconds proved it works. – Brian T Hannan Apr 06 '11 at 02:44
  • 1
    I had to add setOpaque(true) on my JLabel to get this to work. – ban-geoengineering Mar 04 '13 at 23:02
3

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 isSelected and 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.

Goblin Alchemist
  • 829
  • 5
  • 11
  • 1
    He's extending JTextArea so that wont work... (I think you supposed he extended DefaultTableCellRenderer) – Uhlen Apr 05 '11 at 20:26
-1

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?

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167