2012-05-27 66 views
-1

沒有趕上覆選框值這是代碼....JTable中

String Columnas[] = {"Rol","Asignado"}; 
modeloTabla= new DefaultTableModel(null,Columnas);   
objSAP = new SAPFunctionCall(); 
try { 
    rolese = objSAP.listaRoles(); 
    modeloTabla.setNumRows(rolese.size()); 

    for (int i=0; i<rolese.size(); i++){ 
     for(int j=0; j<1; j++){ 
      modeloTabla.setValueAt(((BeanRol)rolese.get(i)).getStrNomRol(), i, j); 
      modeloTabla.setValueAt(Boolean.FALSE, i, j+1); 
     } 
    }  
} 
catch (Exception e) { 
    e.printStackTrace();      
}  

tblRol = new JTable();  
tblRol.setModel(modeloTabla); 

tblRol.getColumnModel().getColumn(1).setCellEditor(new CheckBoxCellEditor()); 
tblRol.getColumnModel().getColumn(1).setCellRenderer(new CWCheckBoxRenderer()); 

現在,當我想捕捉的複選框的值... 例如行0柱1 ...

Object obj; 
Boolean bol; 
obj = modeloTabla.getValueAt(0, 1); 

if (obj instanceof Boolean) { 
    bol = (Boolean) obj; 

if (bol == true) 
    System.out.print("SELECTED"); 
else 
    System.out.print("NO SELECTED"); 

} 

現在,作爲附件一附這2類....

public class CheckBoxCellEditor extends AbstractCellEditor implements TableCellEditor { 
    private static final long serialVersionUID = 1L; 
    protected JCheckBox checkBox;  

    public CheckBoxCellEditor() { 
     checkBox = new JCheckBox(); 
     checkBox.setHorizontalAlignment(SwingConstants.CENTER); 
     checkBox.setBackground(Color.white);    
    } 

    public Component getTableCellEditorComponent(
     JTable table,Object value,boolean isSelected,int row,int column) { 
     checkBox.setSelected(((Boolean) value).booleanValue()); 
     return checkBox; 
    }   

    public Object getCellEditorValue() { 
     return Boolean.valueOf(checkBox.isSelected()); 
    } 
} 

public class CWCheckBoxRenderer extends JCheckBox implements TableCellRenderer { 
    private static final long serialVersionUID = 1L; 

    public CWCheckBoxRenderer() { 
     super(); 
     setOpaque(true); 
     setHorizontalAlignment(SwingConstants.CENTER); 
    } 

    public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) { 
     if (value instanceof Boolean) { 
      setSelected(((Boolean)value).booleanValue()); 
      setEnabled(table.isCellEditable(row, column)); 

      if (isSelected) { 
       setBackground(table.getSelectionBackground()); 
       setForeground(table.getSelectionForeground()); 
      } 
      else { 
       setForeground(table.getForeground()); 
       setBackground(table.getBackground()); 
      } 
     } 
     else { 
      return null; 
     } 
    return this; 
    } 
} 

重複沒有捕獲!

+3

歡迎SO。請1)從源頭刪除每第2(空白)行。 2)圖如何使用代碼格式。 3)對代碼塊使用一致的邏輯縮進。 4)發佈[SSCCE](http://sscce.org/)。 –

回答