• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to give different foreground color for the same cell in JTable?

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
I need to give different foreground color for the same cell in a JTable?
I used the TableCellRenderer to give different color for different rows of a JTable by checking the value of the data in each cell.
Is there any way to give different foreground color for a single cell in a JTable.

Thanks In Advance......


Renjith M
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like I have solved similar problem.

My point was to dash cell background with diagonal lines under the cell value so everything is displayed in cell as usual,
but over diagonal dashes.

The solution was to implement proxy renderer which inherits from JPanel. It contains any custom
renderer as subcomponent of panel.
Before display placed component is switched to invisible,
the panel rendered,
the any other staff rendered over empty panel,
the component switched to visible and rendered with property opaque=false.

Please consider sources for details.

http://code.google.com/p/morena/source/browse/trunk/morena/src/morenoapp/components/common/table/renderer/ProxyPanelTableCellRenderer.java
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

T.V.M Renjith wrote:Hi friends,
I need to give different foreground color for the same cell in a JTable?
I used the TableCellRenderer to give different color for different rows of a JTable by checking the value of the data in each cell.
Is there any way to give different foreground color for a single cell in a JTable.

Thanks In Advance......


Renjith M


Doesn't TableCellRenderer's getTableCellRendererComponent both a row and column parameter? In other words, you know the exact cell.
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob,
I really appreciate your time and help.

Yes, I know the exact cell.

This is the TableCellRenderer class I am using

class CustomTableCellRenderer extends DefaultTableCellRenderer {

protected static Border noFocusBorder = new EmptyBorder(0, 0, 0, 0);

public CustomTableCellRenderer() {
super();
setOpaque(true);
setBorder(noFocusBorder);
}
public Component getTableCellRendererComponent(JTable table,
Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(
table, obj, isSelected, hasFocus, row, column);
Object columnValue = table.getValueAt(row, 1);
String columnValue1 = columnValue.toString();
columnValue1 = columnValue1.trim();
Object columnValue2 = table.getValueAt(row, 0);
String columnValue3 = columnValue2.toString();
columnValue3 = columnValue3.toString();

if (isSelected) {
setBackground(table.getSelectionBackground());
setForeground(table.getSelectionForeground());
} else if (columnValue1.equals("In:")) {
if (columnValue3.indexOf("Error") != -1) {
setBackground(Color.GREEN);
setForeground(Color.WHITE);
} else {
setBackground(Color.WHITE);
setForeground(Color.GREEN);
}
} else if (columnValue1.equals("Out:")) {

if (columnValue3.indexOf("Error") != -1) {
setBackground(Color.RED);
setForeground(Color.WHITE);
} else {
setBackground(Color.WHITE);
setForeground(Color.RED);
}
}
return cell;
}
}





In a cell, I have a string value like ("Red Yellow"). I need to display the first part of the value (Red) in red color (foreground) and the second part of the value(Yellow) in yellow color(foreground).

Rob, thanks in advance.............

Thanks....

Renjith M
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please http://faq.javaranch.com/java/UseCodeTags


As for the answer, you'll need to parse your value into two substrings, then convert each into a Color object (using a lookup or something).
 
Dmitry Mamonov
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's even simpler to solve your problem, try HTML for your values:

<html><span bgcolor="blue">Blue</span> Normal <span bgcolor="red">Red</span>
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob,

I can parse the string into two substring and after that how can I convert that into Color object.
I don't know how to use lookup.
Rob, please send me some links or some hints about how to use lookup, if you have time,

Thanks in advance....

Thanks..
Renjith M
 
Renjith mohanan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dmitry,

Thanks for your time.

I tried the code .



But it is displaying the characters in black color....

I don't know, what is the problem with this.

Is there any other way to do this coloring.


Thanks
Renjith M

 
Dmitry Mamonov
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Renjith.

My example explains how to configure text background color,
to change foreground you may use tag <font> like:

<html><span bgcolor="blue"><font color="red">Red on blue</font><span>
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

T.V.M Renjith wrote: I don't know how to use lookup.
Rob, please send me some links or some hints about how to use lookup, if you have time,


A map comes into mind:
I've used a TreeMap with String.CASE_INSENSITIVE_ORDER so the case of the strings does not matter. Therefore, COLORS.get("red") COLORS.get("RED") and COLORS.get("Red") all return Color.red.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic