ilepper
asked on
How to changing a JTable cell color with non default renderers
Hello,
I have searched the internet high and low, but didnt come up with a solution to
my problem yet, so I hope someone here can help me out a bit.
Ok, the situation, I have a jtable, which uses an AbstractTableModel for handling
the data to be shown in my table. In this model I have overridden the getColumnClass
method so my number cells get aligned nicely to the right.
code:
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
Somewhere in my main code I have grabbed the following code from the internet to
change the eg background color of my cells.
code:
myTable.setDefaultRenderer (Object.cl ass, new DefaultTableCellRenderer() {
public Component getTableCellRendererCompon ent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) {
Component comp = this.getTableCellRendererC omponent(t able,value ,isSelecte d,hasFocus ,row,colum n);
comp.setBackground(Color.r ed);
return comp;
}
});
This also works perfectly. But the big problem is, this only works for the default cell
renderer and doesnt seem to work with my number cells, which apparantly use a
different cell renderer. Same problem with booleans, which are rendered with a check box.
So how can I change the color of these kind of cells?
Thanks in advance for any push in de right direction :)
Kind regards
Ido
I have searched the internet high and low, but didnt come up with a solution to
my problem yet, so I hope someone here can help me out a bit.
Ok, the situation, I have a jtable, which uses an AbstractTableModel for handling
the data to be shown in my table. In this model I have overridden the getColumnClass
method so my number cells get aligned nicely to the right.
code:
public Class getColumnClass(int col) {
return getValueAt(0, col).getClass();
}
Somewhere in my main code I have grabbed the following code from the internet to
change the eg background color of my cells.
code:
myTable.setDefaultRenderer
public Component getTableCellRendererCompon
Component comp = this.getTableCellRendererC
comp.setBackground(Color.r
return comp;
}
});
This also works perfectly. But the big problem is, this only works for the default cell
renderer and doesnt seem to work with my number cells, which apparantly use a
different cell renderer. Same problem with booleans, which are rendered with a check box.
So how can I change the color of these kind of cells?
Thanks in advance for any push in de right direction :)
Kind regards
Ido
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for the quick reply CEHJ, gonna try it and see if it satisfies my needs :)
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
[points to CEHJ]
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
I will leave a recommendation in the Cleanup topic area that this question is:
[points to CEHJ]
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
if (column == yourNumericColumnIndex && comp instanceof JLabel) {
JLabel label = (JLabel)comp;
label.setHorizontalAlignme
}
or something ...;-)