从数据库中填充 jtable 并将 tinyint 转换为 java 中的复选框
Fill jtable from database and convert tinyint to checkbox in java
我如何从 MySQL 获取数据到 JTable 并添加 JTable 列号 3 复选框
这是我的代码
String sql = "select `id`,`CostCentersName`, case when `Status` = 1 then true else false end AS Status from costcenters limit 0,10 ";
Clas.classtable obj = new Clas.classtable();
DefaultTableModel dtm = obj.Query(sql);
datatable.setModel(dtm);
CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
datatable.getColumnModel().getColumn(2).setCellRen derer(checkBoxRenderer);
我看到这个错误
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at test.CheckBoxRenderer.getTableCellRendererComponent(CheckBoxRenderer.java:28)
这是我的 class
public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
public CheckBoxRenderer() {
JCheckBox checkBox = new JCheckBox();
setHorizontalAlignment(JLabel.CENTER);
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
checkBox.setBackground( Color.blue);
}
public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
setBackground(table.getSelectionBackground());
return c;
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected((value != null && ((Boolean) value).booleanValue()));
return this;
}
}
谁能帮帮我?
我终于解决了这个问题
里面class
setSelected((value != null && ((Boolean) value).booleanValue()));
应该更换
setSelected(value != null && (Boolean.valueOf(value.toString()).booleanValue()));
我如何从 MySQL 获取数据到 JTable 并添加 JTable 列号 3 复选框 这是我的代码
String sql = "select `id`,`CostCentersName`, case when `Status` = 1 then true else false end AS Status from costcenters limit 0,10 ";
Clas.classtable obj = new Clas.classtable();
DefaultTableModel dtm = obj.Query(sql);
datatable.setModel(dtm);
CheckBoxRenderer checkBoxRenderer = new CheckBoxRenderer();
datatable.getColumnModel().getColumn(2).setCellRen derer(checkBoxRenderer);
我看到这个错误
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at test.CheckBoxRenderer.getTableCellRendererComponent(CheckBoxRenderer.java:28)
这是我的 class
public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
public CheckBoxRenderer() {
JCheckBox checkBox = new JCheckBox();
setHorizontalAlignment(JLabel.CENTER);
checkBox.setHorizontalAlignment(SwingConstants.CENTER);
checkBox.setBackground( Color.blue);
}
public static final DefaultTableCellRenderer DEFAULT_RENDERER = new DefaultTableCellRenderer();
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (isSelected) {
setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
setBackground(table.getSelectionBackground());
return c;
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelected((value != null && ((Boolean) value).booleanValue()));
return this;
}
}
谁能帮帮我?
我终于解决了这个问题
里面class
setSelected((value != null && ((Boolean) value).booleanValue()));
应该更换
setSelected(value != null && (Boolean.valueOf(value.toString()).booleanValue()));