Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background color change on tablecell

Status
Not open for further replies.

kohinoor2007

Programmer
Mar 21, 2007
69
DE

Hi all,

Have a table in which,I want to render a cell(change background color) upon double mouse click,on that particular cell.Is that possible?.

Did try something as follows:

//Have enabled row selection.I need that & no cell selection

table.setColumnSelectionAllowed(false);
table.setRowSelectionAllowed(true);
table.addMouseListener(new TableCellSelectionAdapter());

Also wrote an adapter class.

class TableCellSelectionAdapter extends MouseAdapter{

public void mouseClicked(MouseEvent e){

int selectedRow = this.table.getSelectedRow();
int selectedCol = this.table.getSelectedColumn();
int clickCount = e.getClickCount();
if(selectedRow!=0 && selectedCol!=0 && clickCount ==2) //Row=0 for headerRow,Col =0 for forst col.
{
TableCellRenderer cellRenderer = ReprintAssemblyView.this.table.getCellRenderer(selectedRow,selectedCol);
Component comp = cellRenderer.getTableCellRendererComponent(this.table, this.table.getValueAt(selectedRow,selectedCol),true ,true,selectedRow,selectedCol);
comp.setBackground(Color.green);

}

}

}




Now the above piece of code changes the background color of the whole row of the cell, on which I have double clicked,and Iam able to see the color change in my double clicked row only after I change the selection to some other row(Not instantly).

Know this is because iam using the default cell renderer.

Instead of the whole row & all the cells background color, being getting changed,want only the double clicked cell to change background color.

Any ideas....

Thanks

 
Concrete question would be,is it possible to change the background color of a cell on a table.

My idea was to use a custom rendered component.But as far as i have seen,it can be applied to a particulat datatype or to a particular column & not to a concrete cell.
 
You do use a custom cell renderer. You'd need to put code in to watch which cell is being rendered (row, col) and apply the background of your choice at that point. Of course, the default background still needs to be applied for all other unaffected cells. Be careful since you don't want to block the selection background colour. Look in the JDK source at how the javax.swing.table.DefaultTableCellRenderer does it. You will probably either want to subclass this class, rather than implement TableCellRenderer.

Tim
 
Hi

I am VERY new to JAVA... but I have been working on something similar. I have a table that alternates row color
depending on some value. This was done to improve readability of the table.

Anyway, I wound up making each cell Object a JTextField.
In the editor I created I used the following to set the color of the cell the user is editing. I realize this may be naive or just really, REALLY BAD... if so, could anyone explain why? And if not, perhaps this also answers the original poster's query. Thanks

public class TableTextEditor extends AbstractCellEditor
implements TableCellEditor ,
ActionListener {
JTextField jtf;
String value="";
JButton button;
JColorChooser colorChooser;
Color firstColor;
public TableTextEditor() {
//Set up the editor (from the table's point of view),
// the only way I could figure out to have the JTextfield remain
// as a JTextfiled instead of a String representation of it
// when attempting to edit.
}

public boolean isCellEditable(EventObject evt) {
if (evt instanceof MouseEvent) {
if (((MouseEvent)evt).getClickCount() == 2) {
return true;
}
}
return false;
}


//Implement the one CellEditor method that AbstractCellEditor doesn't.
public Object getCellEditorValue() {
jtf.setBackground(firstColor);
return jtf;
}

//Implement the one method defined by TableCellEditor.
public Component getTableCellEditorComponent(
JTable table,
Object value,
boolean isSelected,
int row,
int column) {
jtf = (JTextField)value;
firstColor = jtf.getBackground();
jtf.setBackground(Color.CYAN);

return jtf;
}



}
 
Doesn't the DefaultCellEditor already use a JTextField to do cell editing?

kohinoor was wanting cell colour changes on cell selection, not cell editing, but the technique is essentially the same I think. You set colour attributes on the component instance which is being used to render the cells. You can learn a lot about how tables are rendered/edited by downloading and examining the code in Sun's default/abstract rendering classes.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top