I created cellrenderer class for change my rows background. if row's(rate%)colums value is something i try to change background of row. But when i try it. Renderer only change non-Integer colums' background.
Here is my renderer.
public class hucreRenderer extends DefaultTableCellRenderer {
Color orginal=Color.white;
@Override
public Component
getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
Component cell =
super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
int value;
try {
//10th column is a 'rate' column
v=Integer.parseInt(table.getModel().getValueAt(
table.convertRowIndexToView(row), 10).toString());
}catch(Exception e){value=1000;}
if(value<100){
cell.setBackground(Color.red);
else{
cell.setBackground(orginal);
}
return cell;
}
}
and Here is Table Inıt.
String col[] = {"ID","AD","GRUP", "ADET", "Kritik","TELEFON", "TEDARİKÇİ", "ALIŞ", "SATIŞ", "ADRES","RATE(%)"};
urunTablo.setModel(new DefaultTableModel(new Object[0][], col) {
Class[] types =
{ Integer.class, String.class,String.class,Integer.class,Integer.class,String.class,String.class,Integer.class,Integer.class,String.class,Integer.class };
DefaultTableModel t =(DefaultTableModel)urunTablo.getModel();
t.getDataVector().removeAllElements();
t.setColumnIdentifiers(col);
for(Urun u:urunler){
int r=new Integer(u.getRate());
Object row[]={
new Integer( u.getKod()),
u.getAd(),
u.getGrup(),
new Integer( u.getAdet()),
new Integer(u.getKritikAdet()),
u.getTelefon(),
u.getTedarikci(),
new Integer( u.getAlis()),
new Integer( u.getSatis()),
u.getAdres(), r};
t.addRow(row);
}
and intresting thing is. if i use Nimbus look and feel theme i dont come across whit this problem.
and there is a code for nimbus. i use this in main method of this dialog
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
//and the catchs...
Thank a lot alredy now. have a nice day.
Edit:default cell renderer doesnt work for cells which is hold Integer.class. Thats the main problem.
After a lot of reseach and test :) finally i found the problem. Problem is setting the tables default renderer style
I was using this type before(Ex code)
myTable.setDefaultRenderer(Object.class,new MyDefaultCellRenderer());
and this is getting object.class to String.class (in Default)(cause DefaultCellRenderer xD) and the renderer just work on String cells
And i set my renderer again like this
myTable.setDefaultRenderer(Integer.class,new MyDefaultCellRenderer());
myTable.setDefaultRenderer(String.class,new MyDefaultCellRenderer());
Now this is can work for Integer and String cells
So u gonna say what about the Look and feel stuff
I think in Nimbus LAF setDefaultRenderer(Object.class,new MyDefaultCellRenderer())
get all type of Object.
Other LAF's doesnt.
Thanks a lot guys. If you have a extra thought about that please leave comment below. Have a easy work :)