How to create a ChoiceBox in JavaFX where I can select multiple alternatives (as it was a list of checkboxes)?
Related
I want to disable a button when the number of selected rows in a tableview is different from 1 using bind property. Is possible? Thanks and sorry for my english
Use Bindings.size on the selectedIndices list of the selection model:
button.disableProperty().bind(Bindings.size(tableView.getSelectionModel().getSelectedIndices())
.isNotEqualTo(1));
I have a table whose list of visible items are filtered by radio buttons at the top of the table. So let's say the radio buttons are A, B, C - if A if chosen the table will only show items of type A, if B is chosen it will show only type B, etc.
I create a separate TableView instance for each selection because the users will be interacting with these filtered values in the table and editing values etc. I have a custom cell factory for each column in my table as well because rows need to be highlighted/frozen according to user input.
So:
for (all values A.. C) {
createNewTableView();
}
private void createNewTableView() {
TableView tableView = new TableView();
...
MyCustomCellFactory customCF = new MyCustomCellFactory();
customCF.setTableView(tableView);
clmn1.setCellFactory(customCF);
...
}
My problem is that when my custom cell factory fires, even through I specifically tell it which instance of table view it belongs to, the table view instance is not the correct one. It's always just the last one I created. I've verified this by checking the instance id's.
So the question is how can I have multiple table view instances with custom cell factories and ensure that I'm always acting on the right one in my custom cell factory code?
Thanks!
The problem actually had to do with not properly instantiating the table columns for each instance of TableView. I posted a simpler example here - JavaFX tabbed pane with a table view on each tab? - and got the answer I needed.
I've created an application to draw objects on a JavaFX pane (rectangle, paths, custom controls etc..).
Now I want to be able to select those objects to move, copy or group them. The javafx Pane don't have a selectionModel by default and I somehow didn't find out how to implement such function.
If someone got an Idea on how to do this, I'll be grateful
You need to create your own selection model.
First you create a class SelectionModel. In that you put a Set. In order to put nodes into that set, you have to create a mouse handler that adds nodes to the model and removes them from the model depending on shift/ctrl pressed while you click on the nodes.
When you move the selection via mouse, you get the position of the currently clicked on node and instead of moving the single node in the event handler you move all nodes of the list in the SelectionModel.
In order to group them, you need to create a class/collection in which you can store the various nodes you selected. Usually the group is just a parent node. But that varies depending on your requirements.
Copy/Paste is a different matter. You need to create some kind of factory that creates and positions new nodes depending on the nodes in your selection model.
Here's an example with code for a start. It shows you how to select nodes.
I have multiple QGraphicsItemGroup drawn in my QGraphicsScene. I want to obtain the list of selected QGraphicsItemGroup.
I'm selecting using
setDragMode(QGraphicsView::RubberBandDrag);
The selectedItems() function returns a list of selected QGraphicsItem.
Is there any alternative to do the same for groups?
Edit
I have created a list of QGraphicsItemGroup as:
QList<QGraphicsItemGroup *> selectionList;
I want to get the selected items(groups) in this list.
How do I type cast QList<QGraphicsItem *> to obtain selectionList?
If you look at the documentation for QGraphicsItem::ItemIsSelectable, it states: -
The item supports selection. Enabling this feature will enable setSelected() to toggle selection for the item. It will also let the item be selected automatically as a result of calling QGraphicsScene::setSelectionArea(), by clicking on an item, or by using rubber band selection in QGraphicsView.
Since QGraphicsItemGroup is derived from QGraphicsItem, I suspect that by default, a QGraphicsItemGroup does not have this flag selected. So, for each group, set this flag and if you only want the groups returned from the rubber band selection, turn off the flags for all the other items.
I have a need here regarding multiple selection of items in the QTreeView. I have two widgets, QTreeView on left and another QGLWidget on the right. If I do a multiple selection, I must highlight the items in the glwidget. Vice versa, I need to highlight the items in the tree view if multiple selection is done on the glwidget. Currently, I am able to update single item by setting the current index of the tree view. Any ideas on how to update the selection of multiple items on the tree view with multiple selection on glwidget?
You can use the tree view's item selection model (treeView->selectionMode(), of type QItemSelectionModel). It has a signal selectionChanged() you can connect to to receive tree view selection changes and apply them to the GL view.
When receiving selection events from the GL view, you can use QItemSelectionModel::select() to propagate them to the tree view.
To enable multiselection on your treeview, call setSelectionMode( MultiSelection ).
Frank was faster, but I still post my (untested) code example for adding an item to the current selection:
treeView->selectionModel()->select(
treeView->model()->index(row, column, parent),
QItemSelectionModel::SelectCurrent);
There are other selection modes too, see the QItemSelectionModel reference. SelectCurrent is a short-hand for Select | Current, so means update current selection by selecting the given items. It does not mean "select as current selection" (replacing previous selection).
In Python (PyQt6) I do :
ui.my_treeView.setSelectionMode(ui.my_treeView.selectionMode().MultiSelection)
It works well.