aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JXTable - Changing cell forground Colour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JXTable - Changing cell forground Colour" Watch "JXTable - Changing cell forground Colour" New topic
Author

JXTable - Changing cell forground Colour

Ask User
Greenhorn

Joined: Jun 17, 2011
Posts: 3
-- I am trying to change the color of different cells in a JXTable based on their value. If a cell value is Negative it has to be Bold and Red, if not it will be simple Black.
The problem is, i am able to see the changed color of cells only when i select a Row. If i do not select a Row in the table all the cell values are of the same color that is
black. However, the font is bold for cells that should be displayed as red. But the color red is displayed only if i select the rows with negative cell value.

-- Can any one throw some light on this. I have tried my trial and errors. My concepts regarding cell rendered seem to be OK.
-- Please let me know if i am missing any thing or if you need any other information to help me out with this .

-- I am sorry i am pasting the well formatted code, but upon submiting the code become unformatted


JXTable(org.jdesktop.swingx.JXTable) named positionMonitorTable With the Table Properties set as -

-------------------------------------------------------------
positionMonitorTable.setColumnModel(PositionMonitor.getColumnModel()); - Table Model described below

positionMonitorTable.setDragEnabled(true);
positionMonitorTable.setDropMode(DropMode.INSERT_ROWS);
positionMonitorTable.setFillsViewportHeight(true);
positionMonitorTable.setBorder(BorderFactory.createRaisedBevelBorder());
positionMonitorTable.setColumnControlVisible(true);
positionMonitorTable.setHorizontalScrollEnabled(true);
positionMonitorTable.setAutoscrolls(true);
positionMonitorTable.setRowHeight(20);
positionMonitorTable.setHighlighters(HighlighterFactory.createSimpleStriping());
----------------------------------------------------------------------

The TableColumnModel as -
TableColumnModel model = new DefaultTableColumnModelExt();
TableColumn column = new TableColumnExt(VALUE);
column.setCellRenderer(new DecimalSupZFormatRendererTEST);
column.setHeaderValue(VALUE);
model.addColumn(column);

-----------------------------------------------------------------
Now the Detail of DecimalSupZFormatRendererTEST class-

public static class DecimalSupZFormatRendererTEST extends
DefaultTableCellRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;

private static final NumberFormat formatter = NumberFormat
.getInstance(Locale.FRANCE);

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

JComponent comp = null;
if(value != null
&& (value instanceof Double || value instanceof Long)) {
Double val = Double.valueOf(1);
val = (Double) value;
value = formatter.format(value);
if(val.doubleValue() >= 0){
comp = (JComponent) super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
}else{
comp = (JComponent) super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
comp.setFont(new Font("Verdana", Font.BOLD, 11));
comp.setForeground(Color.RED);
}
}
setHorizontalAlignment(SwingConstants.RIGHT);
return comp;
}
}

Ask User
Greenhorn

Joined: Jun 17, 2011
Posts: 3
I got to know that i could use org.jdesktop.swingx.decorator.Highlighter . I would now try to find out how can i highlight my negative numbers in the JXTable with Red color.

It would be great if any one could provide with an example.
Ask User
Greenhorn

Joined: Jun 17, 2011
Posts: 3
OK, finally i found the solution to this issue..

This will not be solved simply using custom cell renderer, Rather Highlighter should be used.. as an Example -

HighlightPredicate predicate = new PatternPredicate("^-","column number in JXTable to search for RegEx","column number to apply the highlighter");
ColorHighlighter highlighter= new ColorHighlighter(predicate ,null,Color.RED,null,null); --- See API Doc for Details
positionMonitorTable.setHighlighters(highlighter);


-- This will color of all the cell whose data starts with a negative sign.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JXTable - Changing cell forground Colour