- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有复选框列的表格,我可以像我想要的那样显示该表格,并且我可以仅编辑放置复选框的列。问题是当我选择复选框时,以正确的方式渲染绘制复选框,但表模型中的值没有更改,该值只是更改,直到我单击另一个复选框,总是我选择的最后一个复选框不会更改体现了他在TableModel中的值(value)。我正在使用的类是:
检查单元格
class CheckCell extends DefaultCellEditor implements TableCellRenderer{
private JComponent component = new JCheckBox();
private boolean value = false;
public CheckCell() {
super(new JCheckBox());
}
@Override
public Object getCellEditorValue() {
return ((JCheckBox)component).isSelected();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
( (JCheckBox) component).setBackground( new Color(200,200,0) );
boolean b = ((Boolean) value).booleanValue();
( (JCheckBox) component).setSelected( b );
( (JCheckBox) component).setHorizontalAlignment(SwingConstants.CENTER);
return ( (JCheckBox) component);
}
@Override
public boolean stopCellEditing() {
value = ((Boolean)getCellEditorValue()).booleanValue() ;
((JCheckBox)component).setSelected( value );
return super.stopCellEditing();
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value == null)
return null;
return ( (JCheckBox) component );
}
}
检查渲染
class CheckRender extends JCheckBox implements TableCellRenderer {
private JComponent component = new JCheckBox();
public CheckRender() {
setOpaque(true);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
((JCheckBox) component).setBackground( new Color(0,200,0));
boolean b = ((Boolean) value).booleanValue();
((JCheckBox) component).setSelected(b);
((JCheckBox) component).setHorizontalAlignment(SwingConstants.CENTER);
return ((JCheckBox) component);
}
}
我的模型
class DynaTableModel extends DefaultTableModel {
public DynaTableModel() {
super();
}
public DynaTableModel(final DynaTableBean dynaBean) {
super(dynaBean.getContent(), dynaBean.getHeaders());
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == 0)
{
return true;
} else {
return false;
}
}
@Override
public void setValueAt(Object value, int row, int column) {
super.setValueAt(value, row, column);
}
你能帮我解决这个问题吗?当复选框中发生此更改时,我缺少什么来更改 jtable 中的值。
我是java新手,我认为有些东西是我所缺少的。提前致谢。
<小时/>更明确一点,当我勾选复选框并尝试从 TableModel 恢复值时,如果我勾选第一个复选框并尝试使用以下方法恢复值,则该值是错误的:
valor =( boolean )tablemodel.getValueAt(i, 0);
当这应该是真的时,我得到了假,但是如果我检查第一个复选框,然后检查第二个复选框,那么第一个复选框的值现在很好(真),但第二个仍然是假,这是一种错误,但我找不到避免这种情况的方法。
有人知道如何避免这种情况。
最佳答案
I have a table with a checkbox column, I am able to show the table like i want and I make editable just the column where the checkbox is placed. The problem is when I select the checkbox the render paint in the right way the checkbox but the value in the tablemodel isn't changed, this value just change until i give click in another checkbox, always the last checkbox that I choose doesn't reflects his value in the TableModel
JTable内置了对 JCheckB
ox 作为 TableCellRenderer
和 Editor
Boolean
值代表 XxxTableModel
中的 JCheckBox
,您需要将 true
或 false
(第一列)
关于java - TableModel 在其他复选框中第二次单击后更改 jcheckbox 单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057445/
我是一名优秀的程序员,十分优秀!