Zolf
asked on
Change colour of cell
Hello there,
i just want a column cell to change colour.but in my case the complete row changes colour.my code is below.thanks
cheers
Zolf
i just want a column cell to change colour.but in my case the complete row changes colour.my code is below.thanks
cheers
Zolf
private class CustomeTableCellRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
System.out.println(row);
//int days=Integer.parseInt(table.getModel().getValueAt(row,creditColumn).toString());
float amt=Float.parseFloat(table.getModel().getValueAt(row,creditColumn).toString());
if(amt>0)
{
comp.setForeground(Color.green);
}else
{
comp.setForeground(table.getForeground());
}
return comp;
}
}
Open in New Window
You're using "creditColumn" to calculate amt, while you're using "column" in you method definition.
only apply the renderer to that column instead of all of them
ASKER
can you both please explain more or give me reference from my code.appreciate your help
ASKER
OK i updated my code.but still i have the same problem
private class CustomeTableCellRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, creditColumn);
System.out.println(row+" "+column);
//int days=Integer.parseInt(table.getModel().getValueAt(row,creditColumn).toString());
float amt=Float.parseFloat(table.getModel().getValueAt(row,creditColumn).toString());
if(amt>0)
{
//comp.setForeground(Color.green);
}else
{
//comp.setForeground(table.getForeground());
}
return comp;
}
}
Wild guess:
change
float amt=Float.parseFloat(table .getModel( ).getValue At(row,cre ditColumn) .toString( ));
to
float amt=Float.parseFloat(table .getModel( ).getValue At(row,col umn).toStr ing());
change
float amt=Float.parseFloat(table
to
float amt=Float.parseFloat(table
And don't comment out the "comp.setForeground(Color. green)
ASKER
i get this exception
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatExce ption: For input string: "abcdef"
at sun.misc.FloatingDecimal.r eadJavaFor matString( Unknown Source)
at java.lang.Float.parseFloat (Unknown Source)
at custom.java.swing.Purchase DetailFram e$CustomeT ableCellRe nderer.get TableCellR endererCom ponent(Pur chaseDetai lFrame.jav a:684)
at javax.swing.JTable.prepare Renderer(U nknown Source)
at javax.swing.plaf.basic.Bas icTableUI. paintCell( Unknown Source)
at javax.swing.plaf.basic.Bas icTableUI. paintCells (Unknown Source)
at javax.swing.plaf.basic.Bas icTableUI. paint(Unkn own Source)
at javax.swing.plaf.Component UI.update( Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatExce
at sun.misc.FloatingDecimal.r
at java.lang.Float.parseFloat
at custom.java.swing.Purchase
at javax.swing.JTable.prepare
at javax.swing.plaf.basic.Bas
at javax.swing.plaf.basic.Bas
at javax.swing.plaf.basic.Bas
at javax.swing.plaf.Component
This means not every cell in your table contains a float value. There's at least one cell with the value "abcdef" in it.
If you need to be able to change the foreground of every single cell in the table you should do a check first to see what column you're in and if it contains a float value.
If you know which column(s) contains a float value, install the renderer simply for that/those column(s). To do this you use:
JTable.setDefaultRenderer( Class<?> columnClass, TableCellRenderer renderer)
so
table.setDefaultRenderer(F loat.class , new CustomeTableCellRenderer() );
In your table model you define the column(s) containing the floats as being of type Float.class.
If you need to be able to change the foreground of every single cell in the table you should do a check first to see what column you're in and if it contains a float value.
If you know which column(s) contains a float value, install the renderer simply for that/those column(s). To do this you use:
JTable.setDefaultRenderer(
so
table.setDefaultRenderer(F
In your table model you define the column(s) containing the floats as being of type Float.class.
ASKER
Bart_Cr:
thanks for your comment.look at the screenshot.to give you a better idia to what i am trying to acheive.
the code is what i get now in the scrren shot.
private class CustomeTableCellRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, creditColumn);
System.out.println(row+" "+creditColumn);
//int days=Integer.parseInt(table.getModel().getValueAt(row,creditColumn).toString());
//float amt=Float.parseFloat(table.getModel().getValueAt(row,creditColumn).toString());
float amt=Float.parseFloat(table.getModel().getValueAt(row,creditColumn).toString());
if(amt>0)
{
comp.setForeground(Color.green);
}else
{
comp.setForeground(table.getForeground());
}
return comp;
}
}
1.JPG
Could you provide the code for your TableModel?
ASKER
purchaseTableModel=new DefaultTableModel(){
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column) {
if(column==3)
{
return true;
}else
{
return false;
}
}
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case debitColumn:
case creditColumn:
return Float.class;
default: return Object.class;
}
}
};
purchaseTableModel.setColumnIdentifiers(new String[]{"Sr.No.","Code","Name","Comment","Debit","Credit"});
table=new JTable(purchaseTableModel);
You can use
table.getColumnModel().get Column(cre ditColumn) .setCellRe nderer(new CustomeTableCellRenderer() );
This at the place you construct your table.
This makes sure only the renderer only works for this one column. You'll need to remove the default cellrenderer for Float.class though
table.getColumnModel().get
This at the place you construct your table.
This makes sure only the renderer only works for this one column. You'll need to remove the default cellrenderer for Float.class though
ASKER
at present i have this what do i do.
table=new JTable(purchaseTableModel)
defaultForeground=table.ge
table.setDefaultRenderer(O
table.setAutoCreateRowSort
>>You'll need to remove the default cellrenderer for Float.class though
where is this
Remove the setDefaultRenderer(Object. class ...)
ASKER
yes that did the trick but the debit column has lost its alignment.see the screenshot.
also can i only change the colour of 900 in credt column to red.
ASKER
screenshot
2.JPG
2.JPG
ASKER
also the column should be creditcolumn or column
private class CustomeTableCellRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 1L;
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column)
{
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
System.out.println(row+" "+creditColumn);
//int days=Integer.parseInt(table.getModel().getValueAt(row,creditColumn).toString());
//float amt=Float.parseFloat(table.getModel().getValueAt(row,creditColumn).toString());
//table.setDefaultRenderer(Float.class, new CustomeTableCellRenderer());
float amt=Float.parseFloat(table.getModel().getValueAt(row,column).toString());
if(amt>0)
{
comp.setForeground(Color.green);
}else
{
comp.setForeground(table.getForeground());
}
return comp;
}
}
The alignment of the debit column is defined by it's class. Because it's a float, java uses a Default renderer for numeric values and thus right alignment. If you don't want this you'll need to change the class of the column or add another custom renderer for the debit column.
In your custom renderer you can check the value and change the foreground depending on the value. Simply add more checks and set it to another color as required.
In your custom renderer you can check the value and change the foreground depending on the value. Simply add more checks and set it to another color as required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
so i create two customcellrenderer.one for debitcolumn and one for creditcolumn.
also can i only change the colour of 900 in credt column to red.how can i do this.
I don't know what your specific requirements are there, but:
if (amt == 900) {
comp.setForeground(Color.r ed);
} else if (amt>0) {
comp.setForeground(Color.g reen);
} else {
comp.setForeground(table.g etForegrou nd());
}
if (amt == 900) {
comp.setForeground(Color.r
} else if (amt>0) {
comp.setForeground(Color.g
} else {
comp.setForeground(table.g
}
ASKER
OK.Thanks
one last question.how can i get the row and column of cell where i am adding these values.the code below is where i add the values from db into the table.
stmt=conn.prepareStatement
stmt.setInt(1,receiptID);
rs=stmt.executeQuery();
while(rs.next())
{
String value = rs.getString(2);
value = value + " (Commission)";
addRowDiscount(rs.getInt(1
null,rs.getFloat(3),0,
purchaseTableModel);
}
I'm not entirely sure what your question is here
ASKER
i want to get the row and column number from the tablemodel purchaseTableModel in the method addRowDiscount
ASKER
Thanks mate for your time and patience
You can't simply get them as you define everything yourself in the tablemodel.