I have a jTable with two columns.First column is set as Boolean(for checkbox) and second column has String value.As I am using Netbeans,checkbox got added into all rows of first column. I tried to add it only for those rows which have value in the second column. I used the code for trying that,
private class CustomCellRenderer extends DefaultTableCellRenderer {
/* (non-Javadoc)
* @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
@Override
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
Component rendererComp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus,row, column);
for(int i=row;i<jTable1.getRowCount();i++){
if(jTable1.getValueAt(i, 1)==null){
jTable1.setValueAt(true, i, 0);
//checkbox.setOpaque(false);
}
}
return this ;
}
}
If I try to set value ‘true’ for all other checkbox in the above for loop it’s working well. How can I set it invisible for rest of the rows.
EDIT:
I am adding my code in this
package e2;
import java.awt.Component;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class JTable_CheckBox extends javax.swing.JFrame {
/** Creates new form JTable_CheckBox */
JCheckBox checkbox=new JCheckBox();
public JTable_CheckBox() {
initComponents();
jTable1.setValueAt("John",0,1);
jTable1.setValueAt("James",1,1);
jTable1.setValueAt("Janet",2,1);
jTable1.setValueAt("Tom",3,1);
jTable1.getColumnModel().getColumn(0).setCellRenderer(new CheckboxCellRenderer());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTable_CheckBox().setVisible(true);
}
});
}
public class CheckboxCellRenderer extends JCheckBox implements TableCellRenderer {
public CheckboxCellRenderer() {
setOpaque(false);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
//here i am trying to set check box invisible,but here i am setting as selected
for(int i=row;i<table.getRowCount();i++){
if(table.getValueAt(i, 1)==null){
table.setValueAt(true, i, 0);
//checkbox.setOpaque(false);
}
}
return this;
}
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
TableCellRenderer are used in tables to paint the various cells of the table reusing the same component. The table automatically sets up the proper context, component bounds and invokes the
getTableCellRendererComponentfor each cell of the table that needs to be painted (the first time the table is displayed all visible cells are painted and then after, upon TableModel notification, the appropriate cells are repainted).So in your TableCellRenderer you certainly do not need to iterate over the cells of the table trying to set values. This just does not make sense. Remember you are implementing the interface
TableCellRenderwhich has a contract you need to respect.From the Javadoc:
So all you need to do is return a component representing the
valueparameter properly. Possibly, you can modify the component to indicate that the cell has the focus or not, and wheter it is currently selected or not. Nothing more, nothing less.Now, coming to your question: if you want to display a checkbox only for rows that have a value implement a TableCellRenderer returning a checkbox and modify its visibility according to the value in column 1 (second column).
This example is working:
PS: I did not take isSelected/hasFocus into account, if you want we can come to that later.