Skip to main content

JTableColumn and TableCellRenderer help

No replies
phibod
Offline
Joined: 2006-03-04
Points: 0

Hi,
Doesnt anyone has tried to set a CellRenderer to a table column ?
My goal is to set one to the first column where all the cells has a background color of grey.
Despite my set for this column, the background color of all its belonging cells is initialized with the background color (white) of the defaultCellRenderer of the my JTable.
My methos is specifyColumn0 in my code

Thanks for you help
Bye
Philip

**** API for JTableColumn
setCellRenderer
public void setCellRenderer(TableCellRenderer cellRenderer)Sets the TableCellRenderer used by JTable to draw individual values for this column.

Parameters:
cellRenderer - the new cellRenderer

***** My code ***********

package gestterri.gui.table;

/*
@(#)TableEdit.java 1.17 07/10/26
*
*/

import javax.swing.*;
import javax.swing.table.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.Color;

import gestterri.utilities.ListMessagesApp;
import gestterri.utilities.ResourceNames;
import gestterri.entities.*;
import gestterri.gui.MessageWindowManager;
import gestterri.managers.file.*;
import gestterri.utilities.*;

public abstract class JTableEdit {

GestTerriEntity entity;

LinkedList currentList;

HashMap currentMap;

ListMessagesApp currentListMessages;

JScrollPane scrollPane;

JTable tableView;

String[] columnNames;
TableColumn tableColumn0;
DefaultTableCellRenderer col0Renderer= new DefaultTableCellRenderer();

GestTerriCsvWriter gestTerriCsvWriter;

GestTerriCsvReader gestTerriCsvReader;

String messDoubleDetected;

Logger logger;

final String pathCsvFiles = ResourceNames.getString("InvokerUI.path_csv_files");
final int columnWidth0 = 10;

public JTableEdit() {

//logger
logger= Logger.getLogger("InvokerUI");

//initialize the list , index of the model
logger.log(Level.INFO,"initialize the list , index of the model");

//data of the model
currentList=new LinkedList();

//index of data model
currentMap= new HashMap();

//list of messages
logger.log(Level.INFO,"initialize the list of messages");
currentListMessages = new ListMessagesApp();
MessageWindowManager.setCurrentListMessages(currentListMessages);

};

public JScrollPane getScrollPane() { return scrollPane; }

public JTable getTableView() { return tableView; }

public GestTerriCsvWriter getGestTerriCsvWriter() { return gestTerriCsvWriter; }

public GestTerriCsvReader getGestTerriCsvReader() { return gestTerriCsvReader; }

public abstract void addRowTableEdit();

public abstract void delRowTableEdit();

public abstract Object[] getRowTableEdit(int row);

public abstract void setRowTableEdit(Object[] rowTable,int row);

public abstract void controle();

protected void doubleDetect() {

String key;
int row;

currentMap.clear();
for (row=0; row < currentList.size();row++) {
entity = currentList.get(row);
key = entity.getKey();
if (!key.equals("")) {
logger.log(Level.INFO,"entity key : " + key);
if (currentMap.containsKey(key))
currentListMessages.add(MessageApp.SEVERE,messDoubleDetected,row+1);
else
currentMap.put(key,entity);
}
}
}

//render specifications for column 0
protected void specifyColumn0() {

// max size and not resizable with gray color
tableColumn0= tableView.getColumn(" ");
tableColumn0.setMaxWidth(columnWidth0);
tableColumn0.setResizable(false);
col0Renderer .setBackground(Color.BLUE);
col0Renderer .setForeground(Color.RED);

tableColumn0.setCellRenderer(col0Renderer);

}

protected void affectTableModel() {

//Create a model of the data.
TableModel dataModel = new AbstractTableModel() {
public int getColumnCount() { return columnNames.length+1; }

public int getRowCount() { return currentList.size();}

public Object getValueAt(int row, int col) {
Object[] rowTerri;

if (col==0)
return (row +1);
else {
rowTerri = getRowTableEdit(row);
return rowTerri[col-1];
}
}

public String getColumnName(int column) {

String noName= " ";
if (column >0)
return columnNames[column-1];
else
return noName;
}

public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}

public boolean isCellEditable(int row, int col) {
if (col == 0 || col > columnNames.length )
return false;
else
return true;
}

public void setValueAt(Object aValue, int row, int column) {
Object[] rowTerri;
rowTerri = getRowTableEdit(row);
rowTerri[column-1] = aValue;
setRowTableEdit(rowTerri,row);
controle();
}
};

// Create the table
tableView = new JTable(dataModel);
this.specifyColumn0();
scrollPane = new JScrollPane(tableView);
//tableView.updateUI();

};

}