gpt4 book ai didi

java - TableModel 在其他复选框中第二次单击后更改 jcheckbox 单元格的值

转载 作者:行者123 更新时间:2023-12-02 06:41:59 26 4
gpt4 key购买 nike

我有一个带有复选框列的表格,我可以像我想要的那样显示该表格,并且我可以仅编辑放置复选框的列。问题是当我选择复选框时,以正确的方式渲染绘制复选框,但表模型中的值没有更改,该值只是更改,直到我单击另一个复选框,总是我选择的最后一个复选框不会更改体现了他在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内置了对 JCheckBox 作为 TableCellRendererEditor

  • 的支持
  • Boolean 值代表 XxxTableModel 中的 JCheckBox,您需要将 true false (第一列)

  • 覆盖public Class getColumnClass(int c) { ,

关于java - TableModel 在其他复选框中第二次单击后更改 jcheckbox 单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19057445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com