File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to give different foreground color for the same cell in JTable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to give different foreground color for the same cell in JTable?" Watch "How to give different foreground color for the same cell in JTable?" New topic
Author

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

Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
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


Personal E-mail: kalliyoorrenjith@gmail.com
Dmitry Mamonov
Greenhorn

Joined: Dec 24, 2008
Posts: 28
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Renjith mohanan
Ranch Hand

Joined: Jun 19, 2009
Posts: 52
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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

Joined: Dec 24, 2008
Posts: 28
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

Joined: Jun 19, 2009
Posts: 52
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

Joined: Jun 19, 2009
Posts: 52
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

Joined: Dec 24, 2008
Posts: 28
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

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to give different foreground color for the same cell in JTable?
 
Similar Threads
doubt in JTable
JTable Cell Selection color
Customizing JTable
JTable
JTable row coloring