If I have a list and a button . And I want that button disabled until an item from that list has been selected , what is the best method to use?
ListView has a property named selectionModel which holds a MultipleSelectionModel object. This selection model has properties/lists that represents what items are selected, if any. There are two modes to this selection model:
SINGLE: Only one item can be selected at a time.
MULTIPLE: Any number of items can be selected at a time.
It might matter what selection mode you're using, but I'm not positive. For single-selection, I'd observe the selectedItem or selectedIndex property. For multi-selection, I'd observe the selectedItems or selectedIndices ObservableList. (Note: While I'm not positive, I'd expect either method to work regardless of the selection mode)
// single selection mode
button.disableProperty()
.bind(listView.getSelectionModel().selectedItemProperty().isNull());
// multiple selection mode
button.disableProperty()
.bind(Bindings.isEmpty(listView.getSelectionModel().getSelectedItems()));
The first option uses the isNull method of the selectedItem property. The method returns a BooleanBinding which binds the disable property of the Button.
The second option uses Bindings.isEmpty which creates a BooleanBinding that will be true when the ObservableList is empty. Like the first option, the disable property is bound to this BooleanBinding.
Use isNull to create a BooleanBinding based on the selectedItem property of the selection model. Use this to bind the disable property of the button:
button.disableProperty().bind(listView.getSelectionModel().selectedItemProperty().isNull());
Related
I have a registration panel where if student, instructor, and course administrator can register so if student is selected it should show something like this when Student is selected:
and should show like this if other two are selected like this when any of other two are selected:
I tried using if condition on the selected item in where I have added those text fields but it seems it only works at the beginning of the program when I run it on the basis of what is pre-selected and does not change when I select other items in JComboBox. Is there a solution to this?
You can achieve this in different ways. One of such ways is to use Action Listeners. A JComboBox object generates an action event when a selection is made (see Handling Events on a Combo Box).
In your case, you need to trigger an event based on the selection made in a combo box. This action should change the visibility of components in your panel, which are simply changing the visible attribute from true to false (or vice versa) depending on the selection made.
I have a cuttingOperationComboBox ComboBox and a method that changes items and value of this ComboBox:
public void changeGlass(Glass newGlass)
{
ObservableList<Operation> list = new FilteredList<Operation>(ProductGlassCuttingUI.this.operationsDB.getOperationsList(), operation -> operation.getOperationType().toString().equals("RE") &&
operation.getGlassThickness() == newGlass.getGlassThickness());
if(!list.contains(this.cuttingOperationComboBox.getValue()))
cuttingOperationComboBox.setValue(list.get(0));
cuttingOperationComboBox.setItems(list);
}
I also have change listener added to cuttingOperationComboBox.valueProperty().
It is fired first time by cuttingOperationComboBox.setValue(list.get(0)); and here everything is fine. But when cuttingOperationComboBox.setItems(list); fires change listener, newValue in it is null although list is not empty. Moreover it happens only when ComboBox is visible. Tobe more precise: cuttingOperationComboBox is displayed in TreeView as a part of TreeCell graphics. As long as tree view node containing combobox is collapsed everything is ok, but when i expand this node and combobox shows, the above problem appears.
Anybody know what i am doing wrong?
Whenever you call setItems() of a ComboBox (or other controls involving list of items), the ComboBox (or the respective control) will always clear all selection. I believe this is the behavior caused by the underlying selection model, and I believe it is the desired behavior for most use-cases.
Without knowing exactly what you are achieving, it's hard to give you an exact solution. If you simply want to retain the value after setting new list, then you might need to keep a copy of the old selection item, and then manually set it in after setItems(). If you don't want the ChangeListener to respond to setItems(), either check for null newValue in the listener, or keep a boolean flag somewhere that would tell the listener that the particular call was triggered by setItems().
I want to be able to write a statement that manually selects an item in a JList, such as:
JList myList = new JList(items);
myList.selectValueAt(index);
Documentation of JList:
The selection state of a JList is managed by another separate model, an instance of ListSelectionModel. JList is initialized with a selection model on construction, and also contains methods to query or set this selection model. Additionally, JList provides convenient methods for easily managing the selection. These methods, such as setSelectedIndex and getSelectedValue, are cover methods that take care of the details of interacting with the selection model. By default, JList's selection model is configured to allow any combination of items to be selected at a time; selection mode MULTIPLE_INTERVAL_SELECTION. The selection mode can be changed on the selection model directly, or via JList's cover method. Responsibility for updating the selection model in response to user gestures lies with the list's ListUI.
In your case:
myList.setSelectionIndex(index);
You need to get the ListSelectionModel from the JList. With that, you can modify the selection:
...
ListSelectionModel sm = myList.getSelectionModel();
sm.clearSelection(); // clears the selection
sm.setSelectionInterval(index, index); // Sets a selection interval
...
While the ListSelectionModel allows fine grained control over the selection behaviour of the JList, JList itself also provides convenient methods like JList.setSelectedIndex() to simply modify the selected elements.
You can use
jList.setSelectedValue(string, rootPaneCheckingEnabled)
or
jList.setSelectedIndex(index);
I would like to persist list item selection like it is on first picture(Circle Fragment Example), so when list item is selected it should stay marked as "selected". What I have is two different project I found on net. On first picture is behavior I want to achieve.
On second image(Fragment Basics) you can see that list item that has been selected do not persist "selected" state. What I don't understand is next:
In both case function that is being called is setItemChecked(position,true) but seems like different behavior is applied.
Check if on your second project the choice mode of your list view is set to choice_mode_single
And if the resource parameter of the ArrayAdapter is android.R.layout.simple_list_item_activated_1
I have a TreeGrid with selection appearence set to checkbox.
TreeGrid resultGrid = new TreeGrid();
resultGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
I want some records to be drawn without these checkboxes(in case record is disabled).
I found property showDisabledSelectionCheckbox, which description says:
Should tree nodes show a disabled checkbox instead of a blank space when selectionAppearance:"checkbox" is set on the treegrid, and a node can't be selected?
How I can make the node "unselectable" except setting :
node.setEnabled(false);
And how this property(showDisabledSelectionCheckbox) works?
I would start here. It looks like you can override canEditCell() on the ListGrid itself to keep someone from interacting with the checkbox. I haven't been able to find a method to hide the checkbox completely, however.
Perhaps setting the showDisabledSelectionCheckbox property to false in conjunction with overriding canEditCell() will get you where you're looking to go.
TreeGrid has selection property that can be set via
resultGrid.setSelectionProperty(propertyName);
So setting this property on TreeNodes will define if nodes could be selected.
By default one can just use "canSelect" property.
So this line of code will disable selection of the particular node.
treeNode.setAttribute("canSelect", false);
And if selection appearance is set to SelectionAppearance.CHECKBOX, checkboxes won't be drawn near nodes which couldn't be selected.
This is the only way I've found.