我的Jtable模型没有使我的布尔值成为复选框

时间:2018-10-10 09:52:43

标签: java jtable jcheckbox

对不起,但是我仍然没有从数据库中得到这个东西,请不要更改为复选框。它只是显示为真还是假

try {

    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT * FROM tblpdareenlist");
    DefaultTableModel model = (DefaultTableModel) viewRecordTable.getModel();
    int x = 0;

    String b, c, d, e, f, g, h, i;
    Boolean a;
    while (rs.next()) {
      a = rs.getBoolean("colAttend");
      b = rs.getString("colNr");
      c = rs.getString("colRank");
      d = rs.getString("colFName");
      e = rs.getString("colMName");
      f = rs.getString("colLName");
      g = rs.getString("colSN");
      h = rs.getString("colUnit");
      i = rs.getString("colETE");
      model.insertRow(x, new Object[]{a,b,c,d,e,f,g,h,i});
      x++;
    }
  } catch (Exception e) {
    JOptionPane.showMessageDialog(rootPane, "Syntax Error\n" + e);
  }

1 个答案:

答案 0 :(得分:0)

如果表模型的JTable方法为该列返回getColumnClass,则Boolean.class将为该列显示一个复选框 [1] 。您正在使用DefaultTableModel,它仅对任何列返回Object.class。该模型只是一个非常简单的模型,不是针对 real 系统的最佳模型,更多用于测试或非常简单的数据。

最好是实现自己的TableModel,以返回给定列的正确类,例如扩展AbstractTableModel

仅用于测试,您可以扩展DefaultTableModel并覆盖getColumnClass方法:

    var model = new DefaultTableModel(0, 3) {
        @Override
        public Class<?> getColumnClass(int col) {
            if (col == 0) return Boolean.class;
            else return super.getColumnClass(col);
        }
    };
    var table = new JTable(model);
    model.insertRow(0, List.of(false, "false", 0).toArray());
    model.insertRow(1, List.of(true, "true", 1).toArray());

    JOptionPane.showMessageDialog(null, table);

从长远来看,对于一个真实的项目,您很快就会被该解决方案所困扰-更好地实施专用的TableModel。一个非常简单(不完整)的示例:

public static void main(String[] args) {
    var model = new MyTableModel();
    var table = new JTable(model);
    model.add(new MyData(false, "false", 0));
    model.add(new MyData(true, "true", 1));

    JOptionPane.showMessageDialog(null, table);
}

static final int FLAG = 0;
static final int NAME = 1;
static final int VALUE = 2;

private static class MyTableModel extends AbstractTableModel {
    private final List<MyData> list = new ArrayList<>();

    void add(MyData data) {
        list.add(data);
    }

    @Override
    public int getRowCount() {
        return list.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Object getValueAt(int row, int col) {
        var data = list.get(row);
        switch(col) {
            case FLAG: return data.flag;
            case NAME: return data.name;
            case VALUE: return data.value;
        }
        throw new IllegalArgumentException("column " + col);
    }

    @Override
    public Class<?> getColumnClass(int col) {
        switch(col) {
            case FLAG: return Boolean.class;
            case NAME: return String.class;
            case VALUE: return Integer.class;
        }
        throw new IllegalArgumentException("column " + col);
    }
}

private static class MyData {
    final boolean flag;
    final String name;
    final int value;
    MyData(boolean flag, String name, int value) {
        this.flag = flag;
        this.name = name;
        this.value = value;
    }
}

[1] JTable Tutorial