Forums Register Login

JTable colored rows or columns

+Pie Number of slices to send: Send
Hello, has anyone any idea how to make a row or a column in a jtable coloured?
Thank you in advance...

Pinda
+Pie Number of slices to send: Send
Create an implementation of TableCellRenderer e.g. by extending DefaultTableCellRenderer
Overide getTableCellRendererComponent() to colour the cells.

D.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
Thanks a LOT, i really appreciate your help.
Aaaaaand ... we're on the march. Stylin. Get with it tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1051 times.
Similar Threads
change the JInternalFrame.focus colour to another colour
Jtable
Images in a JTable
jtable
JTable
JTable
JTable
JTable
Customizing JTable
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:43:56.