Need to edit the using Jcheckbox. I am using JTablebinding to populate data. The expected behavior is to take the inputs from the checkbox and update the original list. I have tried adding both CellEditor and Cellrenderer but the checkbox is not editable. Below is the sample application I wrote. Please help.
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;
}
}
Aucun commentaire:
Enregistrer un commentaire