Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JTable - text color

24 views
Skip to first unread message

Christopher Hood

unread,
Jun 2, 2001, 9:37:22 AM6/2/01
to
I would like to display a table with some of the text highlighted in
different colours.

I've managed to work out how to change a whole column but not individual
cells.

Is there any way to do this?

thanks in advance.


Linda Radecke

unread,
Jun 2, 2001, 11:33:46 AM6/2/01
to

Christopher Hood wrote:

You can write your own TableCellRenderer for this, eg:

class MyColorRenderer extends DefaultTableCellRenderer {

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

super.getTableCellRendererComponent(myTable, value,
isSelected, hasFocus, row, column);

setOpaque(true);

if (isSelected) {
setForeground(myTable.getSelectionForeground());
setBackground(myTable.getSelectionBackground());
} else {

setBackground((((row == 1) && (column == 1))
? (new Color(24, 35, 87, 128))
: Color.white));
setForeground((((row == 1) && (column == 1))
? Color.white
: (new Color(24, 35, 87, 128))));
}

return this;
}
}

Linda
--
I see skies of blue and clouds of white / The bright blessed
day, the dark sacred night / And I think to myself what a
wonderful world. Louis Armstrong

Christian Kaufhold

unread,
Jun 2, 2001, 12:46:11 PM6/2/01
to
Linda Radecke <li...@radecke.ch> wrote:

> Christopher Hood wrote:

>> I would like to display a table with some of the text highlighted in
>> different colours.

>> I've managed to work out how to change a whole column but not individual
>> cells.

>> Is there any way to do this?

> You can write your own TableCellRenderer for this, eg:

[...]

> setOpaque(true);

> if (isSelected) {
> setForeground(myTable.getSelectionForeground());
> setBackground(myTable.getSelectionBackground());
> } else {

> setBackground((((row == 1) && (column == 1))
> ? (new Color(24, 35, 87, 128))
> : Color.white));


Isn't that breaking opacity (It doesn't matter, since the JTable will
be opaque)? Then this is again is a manifestation of this background-
painting vs. opacity chaos, which I believe we did discuss in e-mail.

I don't know an clean fix, short of overriding paintComponent()
(which is low-level, but would be more "correct").


Christian
--
Fastfood für Jehirne, Zinksarg steht parat;
Ding Illusione sinn en Drachebloot jebaad.
Ölteppich op Nerve, Jewesse im Krüzzfüer.
Em Overkill der Zweifel rotlos, wie narkotisiert. BAP, Nöher zu mir

Linda Radecke

unread,
Jun 2, 2001, 3:17:26 PM6/2/01
to

Christian Kaufhold wrote:

> Linda Radecke <li...@radecke.ch> wrote:

> > You can write your own TableCellRenderer for this, eg:

> [...]

> > setOpaque(true);
>
> > if (isSelected) {
> > setForeground(myTable.getSelectionForeground());
> > setBackground(myTable.getSelectionBackground());
> > } else {
>
> > setBackground((((row == 1) && (column == 1))
> > ? (new Color(24, 35, 87, 128))
> > : Color.white));
>
> Isn't that breaking opacity (It doesn't matter, since the JTable will
> be opaque)?

Actually yes, of course, that's right.


> Then this is again is a manifestation of this background-
> painting vs. opacity chaos, which I believe we did discuss in e-mail.

Exactly, when we were discussing the TableHeaderUI and your
Multi-Span-Header you did also mention the design-bug on
the linking of set / isOpaque() with the background-painting.


> I don't know an clean fix, short of overriding paintComponent()
> (which is low-level, but would be more "correct").

Yes, I agree, you are right :)


Linda
--
E wieß Blatt Papier, ne Bleisteff / Jedanke bei dir, setz ich
ahm Finster un hühr / wat sich avvspillt vüür der Düür bess ich
avvrötsch en die Zigg en der et dich für mich nit joov / un mir
ming Levve vürm Daach X op einmohl vüürkütt wie en Stroof.
Do kanns zaubere, BAP

>

Linda Radecke

unread,
Jun 3, 2001, 6:39:45 AM6/3/01
to

Christian Kaufhold wrote:
>> Linda Radecke <li...@radecke.ch> wrote:

> > You can write your own TableCellRenderer for this, eg:
>
> [...]
>
> > setOpaque(true);

[...}

> > setBackground((((row == 1) && (column == 1))
> > ? (new Color(24, 35, 87, 128))
> > : Color.white));

> Isn't that breaking opacity (It doesn't matter, since the JTable will
> be opaque)?

Since we are discussing opacity, do you have an idea what is the
reason not to allow to set alpha-values as primary/secondary colors:

getPrimary1

protected ColorUIResource getPrimary1()


in the Class DefaultMetalTheme


->
Class ColorUIResource

public ColorUIResource(int r,
int g,
int b)


the same way as I can use them by the


Class Color

public Color(int r,
int g,
int b,
int a)


?

Linda
--
Oh just one more and I'll walk away all the / everything you win
turns to nothing today, so just one more just one more go inspire
in me the desire in me to never go home
The Cure, Disintegration


Nancy Suchorebrow

unread,
Jun 3, 2001, 2:51:01 PM6/3/01
to
I have the same problem as Christopher. I tried the TableCellRenderer class
but, again, it seems to affect the entire
column and not the individual cells that I would like to change.

For example I would like to set the background color for only the cell at row
1 and column 1 to red.

Any help would be appreciated.

Linda Radecke

unread,
Jun 3, 2001, 5:30:08 PM6/3/01
to

Nancy Suchorebrow wrote:

> I have the same problem as Christopher. I tried the TableCellRenderer class
> but, again, it seems to affect the entire
> column and not the individual cells that I would like to change.

> For example I would like to set the background color for only the cell at row
> 1 and column 1 to red.

Actually I don't understand your problem, what do you think the line:

>setBackground((((row == 1) && (column == 1))

is doing?

The fact that I use blue instead of red should not bother you,
you can change that yourself, I think:
http://www.radecke.ch/singlecellrenderer.htm

Linda
--
Perched on the writing desk was an alabaster raven that Holly
would have liked."I see you fancy my pet, Mr. Paxton", said
Sverre. "His name is Edgar Allen Poe."
James Morrow, This is the Way The World Ends

Christian Kaufhold

unread,
Jun 4, 2001, 6:19:26 AM6/4/01
to
Linda Radecke <li...@radecke.ch> wrote:

[...]

> Since we are discussing opacity, do you have an idea what is the
> reason not to allow to set alpha-values as primary/secondary colors:

[ColorUIResource doesn't allow alpha values]


Historically, ColorUIResource was introduced during 1.1, where there
were no colors with alpha values (Just like FontUIResource only
supports the 1.1 constructor of Font), so maybe it only hasn't been
adjusted to newer Color/Font capabilities.

On the other hand, the ColorUIResources are implicitly used at many
places. If they were (partly) transparent, virtually no component
could claim it was opaque any more, or only when explicitly checking
its background color has no alpha value. Maybe that would be a good
thing, because then finally isOpaque() and background painting would
be decoupled. I have no hope that that will happen. I would also love
to be able to use any java.awt.Paint as background, not just Color.
Paint backgrounds now always require subclassing.


> --
> Oh just one more and I'll walk away all the / everything you win
> turns to nothing today, so just one more just one more go inspire
> in me the desire in me to never go home
> The Cure, Disintegration

Well, is it as good as I promised?


Christian

Linda Radecke

unread,
Jun 4, 2001, 11:49:02 AM6/4/01
to

Christian Kaufhold wrote:

> Linda Radecke <li...@radecke.ch> wrote:

> > Since we are discussing opacity, do you have an idea what is the
> > reason not to allow to set alpha-values as primary/secondary colors:

> [ColorUIResource doesn't allow alpha values]

> Historically, ColorUIResource was introduced during 1.1, where there
> were no colors with alpha values (Just like FontUIResource only
> supports the 1.1 constructor of Font), so maybe it only hasn't been
> adjusted to newer Color/Font capabilities.

Yes, this is true, using alpha-values is only possible since 1.2, thus
this just seems to be a historical question, you are absolutely right;
On the other hand, I can't consider this being consequently, seen from
a general design-standpoint, or don't you think so? For instance, when
I use alpha-values for background-images (e.g. JDesktopPane), often it
is difficult or almost impossible to achieve the satisfactory result
I am looking for, and I think that this also is a consequence of the
missing support of the alpha-values for ColorUIResources in general.


> On the other hand, the ColorUIResources are implicitly used at many
> places. If they were (partly) transparent, virtually no component
> could claim it was opaque any more, or only when explicitly checking
> its background color has no alpha value.

Of course, this is true.

> Maybe that would be a good
> thing, because then finally isOpaque() and background painting would
> be decoupled. I have no hope that that will happen. I would also love
> to be able to use any java.awt.Paint as background, not just Color.
> Paint backgrounds now always require subclassing.

Exactly, I agree completely, I think the same.

> > --
> > Oh just one more and I'll walk away all the / everything you win
> > turns to nothing today, so just one more just one more go inspire
> > in me the desire in me to never go home
> > The Cure, Disintegration

> Well, is it as good as I promised?

In fact, it is ... I love it :)


Linda
--
Kiss me goodbye pushing out before I sleep / can't you see I try
swimming the same deep water as you is hard "The shallow drowned
lose less than we" you breathe the strangest twist upon your lips
"and we shall be together..."The Cure, The same deep water as you


0 new messages