Scrollpane Policy
I'm having a problem with a cell renderer for my JTable. For some reason,
it is showing vertical scrollbars when it doesn't need them.
I am using the same cell renderer for all three columns of my table but
only the middle column is getting superfluous scrollbars and only on some
cells. The first and third columns show them only when appropriate.
What's really weird is that when I click on one of the cells that
shouldn't have the vertical scrollbars, they disappear (the editor on
that column obviously handles its scrollbars correctly), but suddenly
vertical scrollbars appear in _another_ cell that didn't previously have
them! This new cell is typically one, two or three cells higher in the
same column but sometimes a new scrollbar appears in the THIRD column.
(The scrollpane policy in my cell renderer is set to "AS_NEEDED" and the
scrollbars _will_ go away if the policy is set to "NEVER" but then I also
lose them when I actually want them.)
This behavior, especially the "wandering" scrollbars, is so odd that I
don't know where to begin to look. I suppose there could be problems with
the calculation of the column width or height or the renderer itself
could be messed up in some way.
Does anyone have any thoughts on where I should look? I'm hoping someone
will have seen similar behavior before and will be able to point me in
the right direction.
In the meantime, I'll start on an SSCCE to see if that sheds some light
on the problem....
My renderer is pretty simple and I don't see anything that looks like it
might be a problem but here it is anyway. Perhaps someone here can see a
mistake.
=========================================================================
package foo;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellRenderer;
/**
* @version 1.0
* @since 2011
*/
@SuppressWarnings("serial")
public class PhraseListCellRenderer extends JScrollPane implements
TableCellRenderer, PhraseListConstants {
/** The text area containing the cell. */
JTextArea myTextArea = null;
/**
* This constructor puts a text area within a scrollpane.
*/
public PhraseListCellRenderer() {
super();
/* Create a JTextArea and add it to the JScrollPane. */
this.myTextArea = new JTextArea();
this.myTextArea.setLineWrap(true);
this.myTextArea.setWrapStyleWord(true);
setViewportView(this.myTextArea);
// setHorizontalScrollBarPolicy
(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
//FIXME: Why are superfluous vertical scrollbars coming up on
cells that don't need them?
}
@Override
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {
this.myTextArea.setText(value.toString());
if (value.toString().equals("")) { //$NON-NLS-1$
this.myTextArea.setBackground(Color.RED);
this.myTextArea.setForeground(Color.WHITE);
}
else {
if (row%2 == 0) { //if it is an even-numbered row
if (isSelected) {
this.myTextArea.setBackground
(SELECTED_EVEN_ROWS_BACKGROUND_COLOR);
this.myTextArea.setForeground
(SELECTED_EVEN_ROWS_FOREGROUND_COLOR);
}
else {
this.myTextArea.setBackground
(UNSELECTED_EVEN_ROWS_BACKGROUND_COLOR);
this.myTextArea.setForeground
(UNSELECTED_EVEN_ROWS_FOREGROUND_COLOR);
}
}
else { //odd numbered row
if (isSelected) {
this.myTextArea.setBackground
(SELECTED_ODD_ROWS_BACKGROUND_COLOR);
this.myTextArea.setForeground
(SELECTED_ODD_ROWS_FOREGROUND_COLOR);
}
else {
this.myTextArea.setBackground
(UNSELECTED_ODD_ROWS_BACKGROUND_COLOR);
this.myTextArea.setForeground
(UNSELECTED_ODD_ROWS_FOREGROUND_COLOR);
}
}
}
return this;
}
}
=========================================================================
--
Novice