Check/Uncheck CheckboxCell, onclick of a row - GWT - java

I have a cell table with the first column as checkboxes. My checkboxes have to be checked or unchecked when there is any single click event on the entire row. This is the following code for creating a MultiSelectionModel, creating CheckboxCell and creating a column for cell table.
MultiSelectionModel<Object> selectionModel = new MultiSelectionModel<>();
table.setSelectionModel(selectionModel);
CheckboxCell selectedCell = new CheckboxCell();
Column<Object,Boolean> selectedCol = new Column<Object, Boolean>(selectedCell){
#Override
public Boolean getValue(Object object) {
return object.isSelected();
}
};
table.addColumn(selectedCol);
//Single Click Event to Enable/Disable checkbox.
table.addDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Set<Object> selectedItems = selectionModel.getSelectedSet();
for (Object s : selectedItems) {
Window.alert(String.valueOf(s));
selectionModel.setSelected(s, true);
}
}
}, ClickEvent.getType());
I tried to mark a row as checked using "selectionModel.setSelected(s, true)". But it isn’t working, when I clicked on row, the corresponding checkbox is not being checked.
My question is how do I enable/disable checkboxes onclick of an entire row. Is my approach correct. Or Is there any other way to perform this action in GWT.

You are very close to the working solution.
In the selectedCell you should return the value depending on selectionModel:
return selectionModel.isSelected(object);
This way you are using default multi selection model that selects rows by clicking on them. And the checkbox value comes from the selection model. That's it.
See the working example below:
CellTable<String> table = new CellTable<String>();
final MultiSelectionModel<String> selectionModel = new MultiSelectionModel<>();
table.setSelectionModel(selectionModel);
CheckboxCell selectedCell = new CheckboxCell();
Column<String, Boolean> selectedCol = new Column<String, Boolean>(selectedCell) {
#Override
public Boolean getValue(String object) {
// return object.isSelected();
return selectionModel.isSelected(object);
}
};
table.addColumn(selectedCol);
table.addColumn(new TextColumn<String>() {
#Override
public String getValue(String object) {
return object;
}
});
List<String> values = new ArrayList<>();
for(int i = 0; i < 10; i++)
values.add("Line " + (i + 1));
table.setRowData(values);
You can use standard Ctrl and Shift keys to control selection.

Related

click handler on bottom vaadin

I need to add a click Handler for a column in a table, so if the table has 3 row I need to add a clickhandeler for every row;
But I need also to add a value (to allow me to distinguish between rows) to the click handeler;
In other words:
I have this clickHandler:
btnElimina.addClickListener(new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
System.out.println( "nell handler " );
}
});
and I should want some thing, like this:
btnElimina.addClickListener(new Button.ClickListener(String Val) {
#Override
public void buttonClick(ClickEvent event) {
System.out.println( "nell handler con "+ val );
} });
Is that possible? I've googled on it but didn't find a solution;
Also If I try to use the ClickEvent there seems nothing that I can use to distinguish between rows;
thanks for help
For Vaadin 7, you can find an example in the official documentation in the "Components Inside a Table" section. TLDR: you can use setData() / getData() from the Component to pass identifying information.
The relevant pieces are here:
Table table = new Table();
table.addStyleName("components-inside");
table.addContainerProperty("Details", Button.class, null);
/* Add a few items in the table. */
for (int i=0; i<100; i++) {
// The Table item identifier for the row.
Integer itemId = new Integer(i);
// Create a button and handle its click. A Button does not
// know the item it is contained in, so we have to store the
// item ID as user-defined data.
Button detailsField = new Button("show details");
detailsField.setData(itemId);
detailsField.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
// Get the item identifier from the user-defined data.
Integer iid = (Integer)event.getButton().getData();
Notification.show("Link " +
iid.intValue() + " clicked.");
}
});
// Create the table row.
table.addItem(new Object[] {detailsField},
itemId);
}
I resolved the issue in this way:
public class TableListe extends CustomComponent implements Button.ClickListener{
private String istituto;
public TableListe(String ista) {
//super();
this.istituto = ista;
}
#Override
public void buttonClick(ClickEvent event) {
System.out.println( " nell handler con this.istituto " + this.istituto);
}
}
It seems to work for now;

How to update table cells with new information using JavaFX?

Whenever I click a row, I want to update the table with new information. For some reason when I click it is not doing anything and errors out. Any idea why I can't do this?
public HierarchyItemTreeCellCallback(TreeView<Requirement> treeView, DataModel dataModelIn)
{
super();
treeViewParent = treeView;
dataModel = dataModelIn;
}
#Override
public TreeCell<Requirement> call(TreeView<Requirement> param)
{
TreeCell<Requirement> cell = new HierarchyItemTreeCell();
cell.addEventFilter(MouseEvent.MOUSE_PRESSED,new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
HierarchyTabController hierarchyTabController = new HierarchyTabController(dataModel);
Requirement req1 = new Requirement("ID", "21", 1, REQUIREMENT_TYPE.SOFTWARE);
hierarchyTabController.updateTableRow(req1);
}
});
return cell;
}
Here is the controller logic:
public void updateTableRow(Requirement r)
{
REQUIREMENT_TYPE reqType = REQUIREMENT_TYPE.SOFTWARE;
Requirement req1 = new Requirement("ID", "21", 1, reqType);
ObservableList<Requirement> list = FXCollections.observableArrayList(req1);
name.setCellValueFactory(new PropertyValueFactory<>("name"));
description.setCellValueFactory(new PropertyValueFactory<>("description"));
tableView.getItems().setAll(list);
tableView.setItems(list);
}
This works whenever I do not call the updateTableRow function from the MouseEvent. However, it is not working when I try to call the controller and update the values from the MouseEvent. Any idea what I am doing wrong?

Multiple checkboxes in a single row in gwt cell table

I have created a cell table with checkbox cell in each column. But when i click on any cell, all check boxes are getting selected. My actual requirement is i want to create child parent relationship in checkbox cells in a row i. e. first column will have checkbox and second column will have its multiple children checkboxes and third will have children for each checkbox from second column. I want to select all children on parent selection and if any child gets unselected parent also should get unselected. Parent will remain selected only if its all children are selected.
Please give me a solution. Thanks in advance.
Code is as follows
final SelectionModel<ContactInfo> selectionModel = new MultiSelectionModel<ContactInfo>();
cellTable.setSelectionModel(selectionModel,
DefaultSelectionEventManager.<ContactInfo> createCheckboxManager(0));
Column<ContactInfo, Boolean> checkColumn = new Column<ContactInfo, Boolean>(new NamedCheckBoxCell(true, false)) {
#Override
public Boolean getValue(ContactInfo object) {
NamedCheckBoxCell namedCell = (NamedCheckBoxCell)this.getCell();
namedCell.setTextValue(object.getFirstName());
return selectionModel.isSelected(object);
}
};
cellTable.addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("First Name"));
cellTable.setColumnWidth(checkColumn, 20, Unit.PCT);
cellTable.addCellPreviewHandler(DefaultSelectionEventManager.<ContactInfo> createCheckboxManager(1));
Column<ContactInfo, Boolean> firstNameColumn = new Column<ContactInfo, Boolean>(
new NamedCheckBoxCell(true, false)) {
#Override
public Boolean getValue(ContactInfo object) {
NamedCheckBoxCell namedCell = (NamedCheckBoxCell)this.getCell();
namedCell.setTextValue(object.getLastName());
// Get the value from the selection model.
return selectionModel.isSelected(object);
}
};
cellTable.addColumn(firstNameColumn, "Last Name");
cellTable.setColumnWidth(firstNameColumn, 20, Unit.PCT);
cellTable.addCellPreviewHandler(DefaultSelectionEventManager.<ContactInfo> createCheckboxManager(2));
// Last name.
Column<ContactInfo, Boolean> lastNameColumn = new Column<ContactInfo, Boolean>(
new NamedCheckBoxCell(true, false)) {
#Override
public Boolean getValue(ContactInfo object) {
NamedCheckBoxCell namedCell = (NamedCheckBoxCell)this.getCell();
namedCell.setTextValue(object.getAddress());
return selectionModel.isSelected(object);
}
};
lastNameColumn.setSortable(true);
cellTable.addColumn(lastNameColumn, "Address");
cellTable.setColumnWidth(lastNameColumn, 20, Unit.PCT);

How to get values of grid in GXT?

List<ColumnConfig<Vo, ?>> l = new ArrayList<ColumnConfig<Vo, ?>>();
l.add(numColumn);
l.add(subjectColumn);
l.add(nameColumn);
l.add(dateColumn);
ColumnModel<Vo> cm = new ColumnModel<Vo>(l);
Grid<Vo> grid = new Grid<Vo>(store, cm) {
#Override
protected void onAfterFirstAttach() {
super.onAfterFirstAttach();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
loader.load();
}
});
}
};
grid.addCellClickHandler(new CellClickHandler() {
#Override
public void onCellClick(CellClickEvent event) {
// TODO Auto-generated method stub
contentPanel.clear();
contentPanel.add(readPanel(contentPanel));
}
});`
When I click on cell, I want to get the data in the cell corresponding.
The current state,
When you click on of the cell, switch to a different view of the structure.
And I succeeded to connect to the database.
However, I want to get the data of cell or row.
How to get values of grid in GXT?
(example Site:http://www.sencha.com/examples/#ExamplePlace:paginggrid)
GXT Grid works with data stores, more precisely it is a ListStore I think. So that, to get Values of the grid either use that store by grid.getStore(), and after that you basically have a collection of the objects in your grid (grid.getStore().getAll() return List), or you can use Grid's SelectionModel to deal with the grid selected item like this:
grid.getSelectionModel().addSelectionChangedHandler(new SelectionChangedHandler<Vo>() {
#Override
public void onSelectionChanged(SelectionChangedEvent<Vo> event) {
if (grid.getSelectionModel().getSelectedItem() != null) {
// Do whatever you want
} else {
}
}
});
I hope it will help.
If you want to get the value of a single cell you can try this inside the cellClickHandler :-
ListGridRecord record = event.getRecord();
int colNum = event.getColNum();
String fieldName=grid.getFieldName(colNum);
String cellValue=record.getAttribute(fieldName);
cellValue will have the desired value.

GXT checkbox in grid

I need to update the store when the checkbox in the grid cell changed its state: to add or to remove the value from store. how to handle this event?
BTW, I create the checkbox in grid in this way:
column = new ColumnConfig();
column.setId("accepted");
column.setHeader("Accepted");
column.setWidth(55);
UPD2: Now I do the following:
create checkboxes as firstly decided:
CheckColumnConfig checkColumn = new CheckColumnConfig("accepted", "", 55);
CellEditor checkBoxEditor = new CellEditor(new CheckBox());
checkBoxEditor.setToolTip("If you click here server will consider this rule checking your messages");
checkColumn.setEditor(checkBoxEditor);
checkColumn.setHeader("apply");
configs.add(checkColumn);
than handle events in the grid like this:
UPD3:
grid.addListener(Events.CellMouseUp, new Listener<GridEvent>() {
#Override
public void handleEvent(GridEvent be) {
PropertyItem item;
if (grid.getStore().getAt(be.getRowIndex()).isAccepted()){
item = new PropertyItem(val1, val2, val3, true);
} else {
item = new PropertyItem(val1, val2, val3, false);
}
store.update(item);
store.commitChanges();
saveProperties(store, customerId, toRemove);
}
});
this is the right way.
According to the docs found here, you can add a listener to the CellEditor's Complete event. In the Complete event Listener, perform whatever activity you need to accomplish.
Update:
Try the following
column.setRenderer(new GridCellRenderer() {
#Override
public Object render(ModelData model, String property, ColumnData config, int rowIndex, int colIndex, final ListStore store, Grid grid) {
CheckBox box = new CheckBox();
box.addListener(Events.Change, new Listener<FieldEvent>() {
#Override
public void handleEvent(FieldEvent be) {
st.commitChanges();
saveProperties(st, customerId, toRemove);
// I'm not sure what saveProperties is, but see if this works now.
// this event should DEFINITELY be fired when the checkbox is clicked
// so if it doesn't work, try changing how you do your code here
// maybe by doing model.set(property, (Boolean) be.getValue()); or something
}
});
return box;
}
});

Categories

Resources