This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable colored rows or columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable colored rows or columns" Watch "JTable colored rows or columns" New topic
Author

JTable colored rows or columns

Pinda Ros
Ranch Hand

Joined: Apr 14, 2003
Posts: 65
Hello, has anyone any idea how to make a row or a column in a jtable coloured?
Thank you in advance...

Pinda


NanoAgent is an Artificial intelligence RoBot of microscopic proportions built by means of nanotechnology
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Create an implementation of TableCellRenderer e.g. by extending DefaultTableCellRenderer
Overide getTableCellRendererComponent() to colour the cells.

D.
shahabas shabeer
Ranch Hand

Joined: Feb 23, 2004
Posts: 49
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


The greatest pleasure in life is doing what peoples say you can not do.
Pinda Ros
Ranch Hand

Joined: Apr 14, 2003
Posts: 65
Thanks a LOT, i really appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTable colored rows or columns
 
Similar Threads
jtable
Jtable
JTable
JTable