Hi All,
I have a JTable in which one cell has a JComboBox as cell editor. If a
particular value is chosen in the JComboBox, some other cells in the
JTable should become editable.And i need to show the editable cells in
a different color. My problem is that, though cells are becoming
editable according to the condition, color of those cells are changing
only if click them. I want color to change when selection is made in
the JComboBox. Please find sample program, and give some suggentions.
Thanks in advance
Chanchal
<code>
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.*;
public class JTableTest extends JFrame {
public JTableTest() {
jScrollPane1 = new JScrollPane();
jTable1 = new JTable();
jTable1.setRowSelectionAllowed(false);
TestModel testModel = new TestModel();
testModel.setDataVector(new Object [][] {{"1", "2"},{"3",
"4"},},
new String [] {"Title 1", "Title 2"});
jTable1.setModel(testModel);
JComboBox jcb = new JComboBox(new DefaultComboBoxModel(new
Object[]{"1","2"}));
jTable1.getColumn(jTable1.getColumnName(0)).setCellEditor(new
DefaultCellEditor(jcb));
jTable1.getColumn(jTable1.getColumnName(0)).setCellRenderer(new
TestRenderer());
jTable1.getColumn(jTable1.getColumnName(1)).setCellRenderer(new
TestRenderer());
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1, BorderLayout.CENTER);
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTableTest().setVisible(true);
}
});
}
private JScrollPane jScrollPane1;
private JTable jTable1;
//inner class for TableModel
class TestModel extends DefaultTableModel{
List<Integer> nonEditableColumns;
public TestModel(){
nonEditableColumns = new ArrayList<Integer>();
nonEditableColumns.add(1);
}
public void setDataVector(Object[][] data, Object[] headers) {
super.setDataVector(data, headers);
}
public boolean isCellEditable(int row, int column) {
if(nonEditableColumns.contains(column)){
return false;
}else {
return true;
}
}
public void setValueAt(Object value, int row, int column) {
if(column == 0){
int val = Integer.parseInt(String.valueOf(value));
if(val == 2){
nonEditableColumns.remove(new Integer(1));
}else{
nonEditableColumns.add(new Integer(1));
}
}
fireTableCellUpdated(row, column);
}
}
//inner class for renderer
class TestRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent(JTable jTable,
Object value, boolean selected, boolean hasFocus, int row, int column)
{
TableModel tModel = jTable.getModel();
setOpaque(true);
if(tModel.isCellEditable(row, column)){
setBackground(Color.MAGENTA);
}else{
setBackground(Color.CYAN);
}
setForeground( Color.BLACK );
setText(String.valueOf(jTable.getModel().getValueAt(row,column)));
return this;
}
}
}
</code>
Tom N - 14 Dec 2007 04:28 GMT
> I have a JTable in which one cell has a JComboBox as cell editor. If a
> particular value is chosen in the JComboBox, some other cells in the
[quoted text clipped - 3 lines]
> only if click them. I want color to change when selection is made in
> the JComboBox. Please find sample program, and give some suggentions.
You need to fireTableCellUpdated on the cells whose colour has changed
(i.e. when they stop being editable or become editable). Otherwise they
might not be rerendered.