Java 复选框在jtable列中不可编辑org.jdesktop.beansbinding

Java 复选框在jtable列中不可编辑org.jdesktop.beansbinding,java,swing,checkbox,jtable,Java,Swing,Checkbox,Jtable,需要使用Jcheckbox编辑。我正在使用JTablebinding填充数据。 预期的行为是从复选框中获取输入并更新原始列表。 我尝试添加CellEditor和Cellrenderer,但复选框不可编辑。 下面是我编写的示例应用程序。请帮忙 公共类MyDialog扩展JFrame { 私有JTable表=新JTable(); public MyDialog()抛出HeadlessException { JScrollPane scrollPane=新的JScrollPane(); scroll

需要使用Jcheckbox编辑。我正在使用JTablebinding填充数据。 预期的行为是从复选框中获取输入并更新原始列表。 我尝试添加CellEditor和Cellrenderer,但复选框不可编辑。 下面是我编写的示例应用程序。请帮忙

公共类MyDialog扩展JFrame
{
私有JTable表=新JTable();
public MyDialog()抛出HeadlessException
{
JScrollPane scrollPane=新的JScrollPane();
scrollPane.setViewportView(表);
//将表格添加到框架中
添加(滚动窗格);
此.setTitle(“表格示例”);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
这个包();
此.setVisible(true);
initDataBindings();
postBindings();
}
私有void initDataBindings()
{
//创建人物列表
List people=new ArrayList();
添加(新的数据包装器(true,“John”);
添加(新的数据包装器(false,“Rambo”);
//创建从列表到JTable的绑定
JTableBinding tb=SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ_WRITE,people,table);
//定义要用于列的属性
所选ObjectProperty=ObjectProperty.create();
BeanProperty name=BeanProperty.create(“名称”);
//配置属性映射到列的方式
tb.addColumnBinding(选定).setColumnName(“选定”);
tb.addColumnBinding(name).setColumnName(“姓氏”);
//实现绑定
tb.bind();
}
私有void postBindings()
{
表.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
final TableColumnModel columnModel=table.getColumnModel();
TableColumn column0=columnModel.getColumn(0);
column0.setCellRenderer(新的DefaultTableCellRenderer()
{
JCheckBox checkBox=新的JCheckBox();
@凌驾
公共组件GetTableCellRenderComponent(JTable JTable、对象o、布尔b、布尔b1、int i、int i1)
{
如果(o!=null)
{
DataWrapper=(DataWrapper)o;
checkBox.setSelected(dataWrapper.isSelected());
返回复选框;
}
返回super.getTableCellRenderComponent(jTable、o、b、b1、i、i1);
}
});
}
}
公共类数据包装器
{
私有最终字符串名;
选择私有布尔值;
公共数据包装器(已选择布尔值,字符串名称)
{
this.selected=selected;
this.name=名称;
}
公共字符串getName()
{
返回名称;
}
公选
{
返回选中的;
}
}

我对您使用的第三方类一无所知。我认为没有必要使用它们。基本JDK类将完成这项工作。唯一的变化是,您可能希望为“DataWrapper”类创建一个自定义TableModel。这将允许您指定每列的“列类”,并允许表使用适当的渲染器/编辑器,而无需创建自定义渲染器/编辑器。签出:为创建自定义对象的自定义模型一步一步的示例。是否锁定或可以考虑仅使用JDK中没有任何第三方库的类的解决方案?
public class MyDialog extends JFrame
{
  private JTable table = new JTable();

  public MyDialog() throws HeadlessException
  {
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(table);

    //add the table to the frame
    this.add(scrollPane);

    this.setTitle("Table Example");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    this.setVisible(true);

    initDataBindings();
    postBindings();
  }

  private void initDataBindings()
  {

    // create the peopel List
    List<DataWrapper> people = new ArrayList<>();
    people.add(new DataWrapper(true, "John"));
    people.add(new DataWrapper(false, "Rambo"));

    // create the binding from List to JTable
    JTableBinding tb = SwingBindings.createJTableBinding(AutoBinding.UpdateStrategy.READ_WRITE, people, table);

    // define the properties to be used for the columns
    ObjectProperty<DataWrapper> selected = ObjectProperty.create();
    BeanProperty name = BeanProperty.create("name");

    // configure how the properties map to columns
    tb.addColumnBinding(selected).setColumnName("Selected");
    tb.addColumnBinding(name).setColumnName("Last Name");

    // realize the binding
    tb.bind();

  }

  private void postBindings()
  {
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    final TableColumnModel columnModel = table.getColumnModel();
    TableColumn column0 = columnModel.getColumn(0);
    
    column0.setCellRenderer(new DefaultTableCellRenderer()
    {
      JCheckBox checkBox = new JCheckBox();

      @Override
      public Component getTableCellRendererComponent(JTable jTable, Object o, boolean b, boolean b1, int i, int i1)
      {
        if (o != null)
        {
          DataWrapper dataWrapper = (DataWrapper) o;

          checkBox.setSelected(dataWrapper.isSelected());

          return checkBox;
        }

        return super.getTableCellRendererComponent(jTable, o, b, b1, i, i1);
      }
    });
  }
}

public class DataWrapper
{

  private final String name;

  private boolean selected;

  public DataWrapper(boolean selected, String name)
  {
    this.selected = selected;
    this.name = name;
  }

  public String getName()
  {
    return name;
  }

  public boolean isSelected()
  {
    return selected;
  }
}