Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

JXTable - Changing cell forground Colour

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-- 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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!