我创建了一个JTable并覆盖了返回类型以在第一个Cell中显示JCheckBox,但我显示了true / false值。我怎么能看到Box而不是价值呢?
public JTable table = new JTable(new DefaultTableModel(new Object[]{"", "ID", "Cod. Proveedor", "Empresa", "Nombre", "Responsable", "F. Entrada", "H. Entrada", "F. Salida", "H. Salida", "Nº Tarjeta", "Tipo"}, 0))
{
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
case 4:
return String.class;
case 5:
return String.class;
case 6:
return String.class;
case 7:
return String.class;
case 8:
return String.class;
case 9:
return String.class;
case 10:
return String.class;
case 11:
return String.class;
default:
return String.class;
}
}
};
public JScrollPane sP = new JScrollPane(table);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{false, result.getString("ID").trim(), result.getString("CODPROV").trim(), result.getString("EMPRESA").trim(), result.getString("NOMBRE").trim(), result.getString("RESPONS").trim(), fEntrada, hEntrada, fSalida, hSalida, result.getString("NTARJETA").trim(), result.getString("TIPO").trim(), false });
答案 0 :(得分:0)
在JTable中,如果要在每行的特定列中添加JCheckBox,则只需将该列的columntype添加为boolean,并且每当向表中添加任何行时,只需在其中添加true或false(布尔值)即可单元格。
请参考下面的代码片段,以将列数据类型设置为boolean add复选框。
Class[] columnTypes = new Class[] { Boolean.class, Object.class, Object.class, String.class, Object.class };
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
答案 1 :(得分:0)
JCheckBox check=new JCheckBox();
jTable1.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(check));
尝试使用此代码作为复选框
String aa[]={"yellow","15","50"};
JComboBox j2=new JComboBox(aa);
j2.setEditable(true);
jTable1.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(j2));
如果您不使用comboBox :)