• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable colored rows or columns

 
Pinda Ros
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, has anyone any idea how to make a row or a column in a jtable coloured?
Thank you in advance...

Pinda
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create an implementation of TableCellRenderer e.g. by extending DefaultTableCellRenderer
Overide getTableCellRendererComponent() to colour the cells.

D.
 
shahabas shabeer
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pinda Ros:
Hello, has anyone any idea how to make a row or a column in a jtable coloured?
Thank you in advance...

Pinda

Hai Ros,

You can make the row or column in a JTable colored by setting your own CellRenderer class(implementation of TableCellRenderer interface) for TableColumn.
Try the below given example:
----------------------------
/*
* @author Shahabas E Shabeer
*/
import javax.swing.*;
import java.awt.Component;
import javax.swing.table.*;
import java.awt.Color;
public class ColoredCellTable extends JFrame{
JTable table=null;
String[] columns= {"AA","BB","CC","DD","EE"};
int[] colwidth= {50,50,30,55,60};
ColoredCellTable(){
createTable(columns,colwidth);
String[] a1Row={"xx","yy","zz","kk","ii"};
((DefaultTableModel)table.getModel()).addRow(a1Row);
String[] a2Row={"11","22","33","44","55"};
((DefaultTableModel)table.getModel()).addRow(a2Row);
String[] a3Row={"aa","bb","cc","dd","ee"};
((DefaultTableModel)table.getModel()).addRow(a3Row);
JScrollPane jsp=new JScrollPane(table);
jsp.getViewport().setBackground(Color.white);
getContentPane().add(jsp);
setSize(300,300);
setVisible(true);
}
public void createTable(String[] colname,int colwid[]){
TableColumn[] tabcol=new TableColumn[colname.length];
MyTableCellRenderer mtcr=new MyTableCellRenderer();
DefaultTableColumnModel tabcolmod=new DefaultTableColumnModel();
for(int i=0;i<colname.length;i++){
tabcol[i]=new TableColumn(i,colwid[i]);
tabcol[i].setHeaderValue(colname[i]);
tabcol[i].setCellRenderer(mtcr);
tabcolmod.addColumn(tabcol[i]);
}
MyTableModel mytable=new MyTableModel();
table=new JTable(mytable,tabcolmod);
}
class MyTableModel extends DefaultTableModel
{
public int getColumnCount(){
return columns.length;
}
public String getColumnName(int col){
return columns[col];
}
public boolean isCellEditable(int col,int row){
return false;
}
public Class getColumClass(int col,int row){
return getValueAt(col,row).getClass();
}
}
public class MyTableCellRenderer extends DefaultTableCellRenderer
{
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);
if(row%2==0){
if(column%2==0)
cell.setBackground(new Color(100,200,213));
else
cell.setBackground(new Color(150,250,130));
}
else{
if(column%2==0)
cell.setBackground(new Color(190,220,200));
else
cell.setBackground(new Color(250,130,10));
}
return cell;
}
}
public static void main(String arg[]){
ColoredCellTable table=new ColoredCellTable();
}
}

----------------------------
Cheers,
Shahabas E Shabeer
 
Pinda Ros
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a LOT, i really appreciate your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic