Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problems with a Custom TableCellRenderer  RSS feed

 
Rich Burgis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a table with one column that supports multiline text. Because of that (I think) I can't just extend DefaultTableCellRenderer (since it is based on JLabel).

So I set up a JScollPane containing a JTextArea as shown below (shamelessly ripped off from Sun's source for DefaultTableCellRenderer)

When I add this to my table the cell is blank. When I return just the JTextArea as the renderer it shows up, but I can't scroll.

Does anyone have any suggestions?

Thanks
Rich

package renderers;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.border.*;

import java.awt.Component;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;

import java.io.Serializable;

public class NoteRenderer extends JPanel
implements TableCellRenderer, Serializable
{
private JTextArea noteText;
private JScrollPane scrollPane;
private static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);

private Color unselectedForeground;
private Color unselectedBackground;

public NoteRenderer( )
{
super();

noteText = new JTextArea( 5, 80 );

scrollPane = new JScrollPane( noteText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

GridBagLayout gl = new GridBagLayout( );
GridBagConstraints gc = new GridBagConstraints( );

gc.fill = GridBagConstraints.BOTH;
gc.weightx = gc.weighty = 1.0;
gl.setConstraints( scrollPane, gc );
add( scrollPane );

setOpaque(true);
setBorder(noFocusBorder);
}

public void setForeground( Color c )
{
super.setForeground(c);
unselectedForeground = c;
}

public void setBackground( Color c )
{
super.setBackground(c);
unselectedBackground = c;
}

public void updateUI( )
{
super.updateUI( );
setForeground( null );
setBackground( null );
}

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

if( isSelected )
{
super.setForeground( table.getSelectionForeground( ) );
super.setBackground( table.getSelectionBackground( ) );
}
else
{
super.setForeground( ( unselectedForeground != null ) ? unselectedForeground
: table.getForeground( ) );
super.setBackground( ( unselectedBackground != null ) ? unselectedBackground
: table.getBackground( ) );
}

setFont(table.getFont());

if( hasFocus )
{
setBorder( UIManager.getBorder( "Table.focusCellHighlightBorder" ) );

if( table.isCellEditable( row, column ) )
{
super.setForeground( UIManager.getColor( "Table.focusCellForeground" ) );
super.setBackground( UIManager.getColor( "Table.focusCellBackground" ) );
}
}
else
{
setBorder( noFocusBorder );
}

setValue( value );

return noteText;
}

public boolean isOpaque( )
{
Color back = getBackground( );
Component p = getParent( );

if( p != null )
p = p.getParent();

// p should now be the JTable.
boolean colorMatch = ( back != null ) && ( p != null ) &&
back.equals( p.getBackground( ) ) &&
p.isOpaque( );

return !colorMatch && super.isOpaque( );
}

The renderer is added with

TableColumn column = table.getColumnModel( ).getColumn( 0 );

NoteRenderer renderer = new NoteRenderer( );

column.setCellRenderer( renderer );
 
Rich Burgis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the code I posted will display but without scrolling. When you change the return from getTableCellRendererComponent to this, nothing displays.

I tested it in the context of a panel on a test JFrame and the scroll is there.

Thanks
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, the renderer is only used to *paint* the cell - it can't be used for interactive things such as scrolling. Only the cell editor can do that.

With other words, if I understand correctly, JTable doesn't support the behaviour you want at all.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see reply 4 here

http://forum.java.sun.com/thread.jspa?threadID=654439
 
Rich Burgis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, that's a great help.

Rich
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!