问题描述
需要编辑使用JcheckBox。我正在使用JTablebinding填充数据。 预期的行为是从复选框中获取输入并更新原始列表。 我尝试同时添加CellEditor和Cellrenderer,但是该复选框不可编辑。 下面是我编写的示例应用程序。请帮忙。
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;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)