Wicket : how to set checked value in CheckBoxMultipleChoice - java

I am using wicket's CheckBoxMultipleChoice to let the user set a list of options. so far it works fine. but then i want to add a "check all" check box that checks all the options in the CheckBoxMultipleChoice and I am having problems with that.
here is my initial code
ArrayList<String> chosen;
List<String> choices = Arrays.asList(new String[]{"Train", "Bus", "Car"});
CheckBoxMultipleChoice myCheck = new CheckBoxMultipleChoice("transport", new Model(chosen), choices));
myCheck.setOutputMarkupId(true);
form.add(myCheck);
on submit i print out the values of chosen and its "Bus", "Car" etc. as expected.
now i am adding a checkbox to check all the choices using ajax:
Boolean checkOrNot;
final CheckBox checkAll = new CheckBox("checkAll", new Model(checkOrNot));
form.add(checkAll);
checkAll.add(new AjaxFormComponentUpdatingBehavior("onchange") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
// here i am not able to set the checkboxes
// i tried doing this
chosen.clear();
chosen.add(new String("Car"));
chosen.add(new String("Train"));
myCheck.modelChanged();
// i have also tried recreating the multiple choice
myCheck = new CheckBoxMultipleChoice<T>("transport", new Model(chosen), choices);
myCheck.setOutputMarkupId(true);
target.addComponent(myCgecj);
target.addComponent(form);
}
});
I am running out of ideas and wondering if anyone has any solutions? thanks in advance for any help.

I didn't (and can't) try that right now, so this is to be taken with a grain of salt, but couldn't you use a CheckGroup for that?
From the JavaDoc:
Component used to connect instances of Check components into a group. Instances of Check have to be in the component hierarchy somewhere below the group component. The model of the CheckGroup component has to be an instance of java.util.Collection. The model collection of the group is filled with model objects of all selected Check components.
So a
new Checkgroup("group", choices)
should work for you. No need to reimplement that functionality.
P.S.: I'll check that as soon as I've got the chance to do so...

You could use javascript to mark the checkboxes.
An example, using jQuery:
mycheck.setOutputMarkupId(true);
checkAll.add(new SimpleAttributeModifier("onclick",
"$('#" + mycheck.getMarkupId() + " input:checkbox').attr('checked', $(this).is(':checked'))");

Well, another way to go is to use
checkBoxMultipleChoice.setDefaultModelObject(listOfAllElements); // Select all.
checkBoxMultipleChoice.setDefaultModelObject(Lists.newArrayList()); // Deselect all.

I know that you fount already an answer but in my case I had exactly the same issue, but the option to use the CheckGroup was not feasible because my list is built dynamically and at the beginning it can even be empty.
The situation for me was:
I was obliged to use the CheckBoxMultipleChoice because it allows me to have empty lists thus reflecting in the GUI. CheckGroups will always print one check box without label, even if the options for the component are empty.
Using AjaxFormComponentUpdatingBehavior would throw an exception saying it was only possible to use it in other components.
Using OnChangeAjaxBehavior would work with the selector CheckBox (select/deselect) but if used with the CheckBoxMultipleChoice then the event will fire but the actual values of the component will not be reflected in the model within the event.
Thanks god I found This Forum in Nabble.
Basically here Pedro Santos propose to use the AjaxFormChoiceComponentUpdatingBehavior instead. I followed the advice and finally I was able to get the values in the model.
Sometimes I think Wicket is Wicked :P

Related

What is a good practice to handle a lot of checkboxes in JavaFX

I need to build a View which has a 4 layer nested Multi-Accordion with a lot of checkboxes inside them. All together there might be around 30-40 Checkboxes all through the Accordions.
The next step will be, that i have some sort of logic behind all this. Depending on the selection combination of the checkboxes I will change a text label accordingly.
My thought process was: I put up all these checkboxes and give them a numeric fx:id representing there position in the nested accordion graph. Something like "1_1" or "2_4_1".
After that, I build one ChangeListener calling a Method on Selection of a Checkbox. I can look up the Id of the checked box, look it up in my data (to see which Text belongs to it and if any rules interfer with other boxes) and handle the logic accordingly while putting the id and its text in a Map or List to keep it for later and to keep track of the checked boxes.
Now I came to know, that getting the fx:id isnt something JavaFX wants me to do. I cant deliver a custom id in custom property inside the FXML either (couldnt find anything regarding this).
I am now pretty much at the end of my knowledge (I did just start with JavaFX and have some basic Java knowledge) and it seems to me, that I tackle this topic from the wrong side.
My question is now: What would be a best practice to handle dozens of checkboxes and trigger logic in the code according to the box that was checked without writing a ChangeListener for every single Check Box leaving me with some (imo) ugly code all the way.
EDIT: I forgot to mention: I did achieve some sort of functional solution by writing a custom CheckboxChangeListener with a reference to the Element the addListener method was called on and using "getId()" on this reference. I came to know though, that this method references the css:id of the fxml element and not its fx:id and I am not quite sure if this is a proper way to go
You should look into databinding with javafx. For example:
CheckBox cb1 = new CheckBox("1");
CheckBox cb2 = new CheckBox("2");
BooleanProperty isCb1Selected = cb1.selectedProperty();
BooleanProperty isCb2Selected = cb2.selectedProperty();
Textfield foo = new TextField().visibleProperty().bind(isCb1Selected.and(isCb2Selected));
This would hide the textfield foo if atleast one of the checkboxes isn't selected.
You can find other examples here and here an oracle tutorial

Java swing toggle button to filter jtable rows

I have a JTable, it contains a custom AbstractTableModel that return an object when getValueAt is called. And of course I have a custom TableCellRenderer that is capable of getting/constructing the text from the object so that it can be displayed.
However now I would like to write a filter. Filter will be a simple toggle button. When it is turned on I would like the filter to be applied and when it is turned off I would like filter to be removed.
I have two problems due to that.
First one is that I have absolutely no idea how to write a filter when you have to filter by object rather than a primitive.
Second is I have no idea how to attached the said filter to the toggle button to be able to turn it on and off.
I am sorry if this is a retarded question but http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#sorting was the most useless documentation I saw since explanation was not in depth.
Thanks to anyone for their help.
EDIT:
The object contains multiple fields, but I am interested in two filter toggles specifically. One returns a boolean value when I say isSuper, and the second return a string when I call getName. If first toggle is turned on it should show all entries that return true on isSuper, and the second toggle should show all entries where name is compromised of two words (space is present) when I call getName.
To be honest, the JavaDocs spell it out quite well...
With such little information to go on, the best you can hope for is an overview...
TableRowSorter<TableModel> trs = new TableRowSorter<TableModel>();
table.setRowSorter(trs);
// Create the row filder...
trs.setRowFilter(new RowFilter<TableModel, Integer>() {
#Override
public boolean include(RowFilter.Entry<? extends TableModel, ? extends Integer> entry) {
boolean include = false;
// Returns the value for the specific column...
CustomObject value = (CustomObject)entry.getValue(filterColumn);
if (filterBySuper) {
include = value.isSuper();
} else {
include = value.getName().startsWith(fistPart) && value.getName().endWith(lastPart);
}
return include;
}
});
When you want to update the filter, you simply need to call...
trs.sort();
First one is that I have absolutely no idea how to write a filter when you have to filter by object rather than a primitive.
Did you read the RowFilter API? It shows an example of how to create a custom filter based on a custom Object.
Second is I have no idea how to attached the said filter to the toggle button to be able to turn it on and off.
Did you read the tutorial and try running the demo? The tutorial uses a DocumentFilter to change the filter dynamically every time the user changes the text in the text field. So the tutorial shows you how to dynamically changes the filter based on user input. You can modify the code to change the filter every time the toggle button is pressed.
it contains a custom AbstractTableModel that return an object when getValueAt is called. And of course I have a custom TableCellRenderer that is capable of getting/constructing the text from the object so that it can be displayed.
Unrelated to my answer, but I don't really understand that statement. Are you saying every cell in the model returns a differently object, or does every cell return the same object but you just display a different property of the object for column1, column2, column3 etc? Either way it sounds like a weird design. We can probably suggest something better. Post your SSCCE that demonstrates the problem.

Get all selected checkboxes on button click [duplicate]

I have a dialog in Java that presents ~ 15 checkboxes to the user. Is there a way to get the names of all the checked checkboxes at once? Currently, I'm looking one by one if they are selected, which isn't that fancy of a solution.
I'm looking for something similar to Getting all selected checkboxes in an array but then in Java
When you are adding your Checkboxes to your dialog also keep a reference in a Collection of some sort. Then when you want to see which are checked you can just Iterate over the collection and check the state of each of them. You can get the name by calling getText on it.
List<JCheckBox> checkboxes = new ArrayList<JCheckBox>();
for( Component comp : panel.getComponents() ) {
if( comp instanceof JCheckBox) checkboxes.add( (JCheckBox)comp );
}
This assumes all of the JCheckBox instances are a direct child of the container panel. If not then you'd need to recursively visit all the containers of panel using the same logic. Now, while you can do this it's typically better to save these references as you created them into a list. Then you can easily iterate over all of the checkboxes without having to do this code above. If you have embedded components it's better to ask the embedded component to perform whatever operation you want over the checkboxes it owns (as opposed to pulling them out of the component through a getter so you can mess them in some way).

SWT ComboBoxCellEditor editable

I have a TableViewer where the values in one column should typically come from a dynamic list.
I'm currently using org.eclipse.jface.viewers.ComboBoxCellEditor , which is actually a Select-List: it stores the index of the selected value. If I change the underlying list (calling setItems(String[]), it's clumsy to keep the previous selected value... (specially if it's not included in the list anymore!) What I'd wish is actually a cell editor that stores, not the index from the list, but the string (perhaps letting the user edit it freely, perhaps not), where the list is just used as a suggestion at input time - like a "combobox" was supposed to work in the good old days... Is this possible?
I would suggest you to have your CellEditor to mimic the behavior that you are looking for. Extend ComboBoxViewerCellEditor and override doGetValue() method. Add modify listener on Combo control and also filter (which filters list items based on input text) to comboviewer.
You should look at :
org.eclipse.wst.xml.ui.internal.properties.StringComboBoxCellEditor This class comes from WTP project; It's an extended ComboBoxCellEditor that selects and returns Strings.
codemirror.eclipse.ui.xquery.viewers.StringComboBoxCellEditor It's the copy/paste of WTP StringComboBoxCellEditor; it adds the capability to add the item in the combo when it is not found.

GWT Cell Tree, Selecting too many options!

I am using a gwt cell tree and I want only one node to be selected in the whole tree but many nodes are being selected.
I am also trying this
S1= new SelectionModel();......
S1.setSelected(S1.getSelected(),false); but using this technique nothing is being selected.
I am having the following problem:
Can someone help??
Maybe SingleSelectionModel helps you;
SingleSelectionModel selectionModel = new SingleSelectionModel();
See Google Example 2
to SingleSelectionModel usage.
Code looks weird to me, because you firstly get selected object with S1.getSelected() command and then kinda re-select same object, so no wonder that nothing happens. Instead S1.getSelected() pass object that you want to select.
You need to provide a key provider to the selection model like this
selectionModel = new SingleSelectionModel<NamedObject>(new ProvidesKey<NamedObject>() {
#Override
public Object getKey(NamedObject item) {
return item.getKey();
}
});
This will uniquely identify the nodes in the tree.

Categories

Resources