My program can add or update profiles. In the update profile dialog, after selecting which profile u want to update. Fields should fill with selected profile values. The problem starts with filling the CheckboxGroup field. (My CheckboxGroup field name is listBox so after that I'm gonna call it that way). when I check my listBox selected items I can see the listBox selected values are true but in the frontend, I can't see any selection. So basically I want to reselect lists in addValueChangeListener but CheckboxGroup not rerendering.
listBox = new CheckboxGroup<>();
listBox.setItemLabelGenerator(Lists::getListName);
listBox.setItems();
listBox.setItems(listsList);
listBox.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
listBox.setHeight("200px");
listBox.getStyle().set("overflow","AUTO");
listBox.setHelperText("Seçilen Listeler");
listBox.addThemeVariants(CheckboxGroupVariant.LUMO_HELPER_ABOVE_FIELD);
updateProfile.addValueChangeListener(e -> {
setUpdateProfileValues();
});
}
private void setUpdateProfileValues() {
if (updateProfile.getValue() == null)
return;
updateProfileName.setValue(updateProfile.getValue().getProfileName());
simAlgorithm.setValue(updateProfile.getValue().getSimAlgorithm());
searchType.setValue(updateProfile.getValue().getSearchType());
operationType.setValue(updateProfile.getValue().getOperatorType());
simPercentage.setValue(updateProfile.getValue().getSimPercentage());
numberOfResult.setValue(updateProfile.getValue().getNumberOfResults());
List<Lists> selectedLists = updateProfile.getValue().getLists();
selectedLists.forEach(lists -> log.error(lists.getListName()));
//selectedLists.forEach(listBox::select);
listBox.setValue(new HashSet<>(selectedLists));
listBox.getSelectedItems().forEach(selectedList -> log.error(selectedList.getListName()));
}
Here is my related code piece for this question. BTW when I check the logs I can see listBox has true selected values. But I need to show on frontend page.
I ran a quick piece of code using your listBox initialization and I was able to set all of the Checkboxes to true on the front end. This might help you:
listBox.getChildren().filter(Checkbox.class::isInstance)
.map(Checkbox.class::cast)
.forEach(c -> c.setValue(true));
This retrieves all the Checkboxes within the CheckboxGroup, checks that they are an instance of the Vaadin Checkbox component, and then sets their T/F value as desired.
Related
I am trying to create a small application where I have various file locations stored in a Vaadin grid, the grid currently only shows two columns - ID and File Name, and I would like to see as a notification the file location too whenever I click on a row.
Meaning, every time I click a row from the grid I would like it to show me the location, using Notification.show(), like that:
(Please ignore the <b></b>s, they are irrelevant.)
As my grids selection model is MULTI, per default the click listener cannot register which row it is clicked on and selecting the row via checkbox is not what I want to have the data displayed. Simply, I would like to get the item for every row I click and have the location displayed as a notification.
So far, I found a solution for a similar issue but it deals with columns and the rows in Vaadin grids are rather different from what I am used to.
public static <T> void addColumnClickListener(Grid<T> grid, Consumer<Column<T>> listener)
{
String expression = "function(){const col=element.getEventContext(event).column;return col ? col.id : '';}()";
grid.getElement().addEventListener("click", e ->
{
String colId = e.getEventData().getString(expression);
Optional<Column<T>> column = grid.getColumns().stream().filter(col -> colId.equals(col.getId().get())).findFirst();
column.ifPresent(listener);
}
).addEventData(expression);
}
And I am to call the function like that:
addColumnClickListener(grid, column -> Notification.show("fubar"));
This code snippet is from the Vaadin forums and I do not quite understand it. The string expression seems to be that it contains possible JavaScript code and the rest overrides the type of the column. (I think, I really do not understand this snippet fully)
Is there any way to do something similar to the snippet above but for rows?
You can do this with an ItemClickListener on the Grid:
grid.addItemClickListener(item -> {
Notification.show(String.format("File location: %s", item.getLocation()));
});
I'm building a log reader with JavaFX as a side project and have gotten to a dead end when trying to implement filtering on the TableView.
What I have is a few CheckBoxes (LHS of the picture) that will basically act as the filters for what the TableView will display:
Once the Submit button is clicked, a background thread is opened to read the and parse the files. Once the operation terminates, the result of each log read is inserted into the global ObservableList<Log>:
public class Test_Filters extends Application {...
private ObservableList<LogTest> logs = FXCollections.observableArrayList();
...}
What I'm having trouble with is how to deal with:
A situation where more than one filter CheckBox is checked.
A situation where the CheckBox is unchecked.
For 1., I was wondering what's the best way to deal with this. Let's say I have x filters selected. This would mean that I have to basically filter out x values from the ObservaleList:
logTable.setItems(logTable.getItems().filtered(log -> !log.getSource().equals(checkBox.getText())));
You can use JavaFX's FilteredList, which accepts a Predicate. You can update the predicate on each filter, combining them as you want.
FilteredList<LogTest> items = new FilteredList<>(originalItems);
tableView.setItems(items);
... on update of filter UI items
Predicate<LogTest> containsFoo = i -> i.getName().contains("foo");
Predicate<LogTest> isSevere = i -> i.getLevel() == Level.SEVERE;
Predicate<LogTest> filter = containsFoo.or(isSevere);
items.setPredicate(filter);
If you want to show all records again simply set the predicate to null:
items.setPredicate(null);
Using the FilteredList you don't need to re-read the log records, as the filter is applied immediately on the existing items.
I am trying to make a filter for a searchfield where a number of checkboxes can be checked to choose what people want to search. I am currently trying to do this with the CheckGroup component but as I do not have a submit button I do not know how I can retrieve the latest checked objects. One thought of doing it was using Javascript, to call a function in Javascript and retrieve all the checkboxes like that. I currently have the following code in Wicket. So my question would be how to do this and if it is possible to not do this with Javascript. I have tried using AjaxFormChoiceComponentBehaviour and that works but since it does a post whenever a checkbox is checked, I think JS would be a better option.
public Filter(String id) {
super(id);
form = new Form("filterform");
types = resultItemHandlerPool.getTypes();
checkGroup = new CheckGroup<Class<?>>("checkGroup", new PropertyModel<Collection<Class<?>>>(this,"types"));
ListView typesListview = new ListView<Class<?>>("typesList", new PropertyModel<List<? extends Class<?>>>(this,"types")) {
#Override
protected void populateItem(final ListItem<Class<?>> item) {
item.add(new Check<Class<?>>("check", item.getModel()));
item.add(new Label("className", item.getModelObject().getSimpleName()));
}
};
typesListview.setReuseItems(true);
checkGroup.add(typesListview);
form.add(checkGroup);
add(form);
}
public List<Class<?>> getSearchableTypes() {
return types;
}
Thanks and kind regards,
Merlijn
You say you want to do the search server side. So, the server needs to know which items are checked in order to do the search.
Just use a plain old form for the searchfield (including checkboxes) and make it so that after entering the search-value the user posts the form. That way, the serverside code will receive the search value and the list of checked checkboxes and will know exactly what to search for.
AjaxFormChoiceComponentBehaviour does indeed update the server side Checkgroup after every click with an ajax post. If you only need to know the value of the Checkgroup after posting the search value, just don't use the AjaxFormChoiceComponentBehaviour and submit the form. Both a normal form submit and an ajax submit will work here.
In my UI i have 2 text field and and 2 buttons .I am using jface data binding to bind the text field and i am doing the validation and depending on the validation success the model is updated other wise it will not.I want my button to respond accordingly .Like if my model is not updated than i want to disable my button.One more thing that i do anot want to do hard coding .so is there any way to that without hard coding.
In other way I want to bind a button to text field so that when the text field has some unwanted value than the button should be disabled .In the other way i am doing data binding on text field which will take care when the text field does not have proper value than it will not update its model.Then i want to disable the button when the value is not proper can i do that.Any pointer on this helps me a lot.
You can make use of below listener. Add listener to your Observable
org.eclipse.core.databinding.observable.value.IValueChangeListener
After some research, I found that I have to observe the enable/disable property of the button and bind it with the current emf databinding context. Detail of the function which I have used is given below:
void bindEnablementButtonToValidationStatus(Button button, EMFDataBindingContext ctx) {
IObservableValue buttonEnable = SWTObservables.observeEnabled(button);
IObservableValue validationStatus = new AggregateValidationStatus(
ctx.getValidationRealm(),
ctx.getBindings(),
AggregateValidationStatus.MAX_SEVERITY);
ctx.bindValue(buttonEnable, validationStatus,
new EMFUpdateValueStrategy(
UpdateValueStrategy.POLICY_NEVER),
new EMFUpdateValueStrategy().setConverter(
new Converter(IStatus.class, Boolean.TYPE) {
public Object convert(Object fromObject) {
return new Boolean(((IStatus)fromObject).isOK());
}
}));
}
as seen in:
http://www.zkoss.org/zkdemo/combobox/simple_combobox
When the item is selected only text appears.
How can I make the image + name appear when the item is selected?
which method from Combobox do i need to override?
ty
There is no build-in way for you to do things like that. Because a Combobox is just a textbox with a customizable drop-down list.
But this can be done pretty easily. You can just add an Image at left of the combobox, and listen combobox Events.ON_SELECT event. Then update the Image according to selected item.
Here is an example code (assuming use SelectorComposer)
#Listen("onSelect = combobox#mycmb")
public void onComboboxSelected(SelectEvent event) {
Set<MyObject> selectedObjects = event.getSelectedObjects();
MyObject obj = selectedObjects.get(0);
image.setSrc(getImagePath(obj)); // image are Image component you wired.
}
Note : I didn't test the code, but you should get the idea