#javafx: hide one ListView item by it's property?

1 messages · Page 1 of 1 (latest)

sinful moss
#

There is object with SimpleBooleanProperty hided field, cell factory, and context menu, one of menuitems sets this property to true.

Question: how to filter/hide this item?

class Element {
    val hided = SimpleBooleanProperty(false)
}

class MyController {

    @FXML
    private lateinit var pckList: ListView<Element>

    fun initialize() {
        pckList.setCellFactory { _ ->
            object : ListCell<Element>() {
                init {
                    contextMenu = ContextMenu(
                        MenuItem("Hide this packet").apply { setOnAction { item.hided.set(true) } },
                    )
                    managedProperty().bind(visibleProperty())
                }

                override fun updateItem(item: Element?, empty: Boolean) {
                    super.updateItem(item, empty)
                    if (item == null) {
                        graphic = null
                        return
                    }
                    graphic = Label("blahblahblah")
                }
            }
        }
    }
}

I tried to setVisible(false), it hides the content, but leaves empty gap between neighbour items...

distant craneBOT
#

<@&1008423204219531294> please have a look, thanks.

sinful moss
#

Also i tried FilteredList, but it causes an error

        pckList.items = FilteredList(FXCollections.observableArrayList({ pck -> arrayOf(pck.hided) }), { pck -> !pck.hided.get()})
Exception in thread "JavaFX Application Thread" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:148)
    at java.util.AbstractList.add(AbstractList.java:108)
    at io.github.relvl.javafx.controller.TabPacketLogController.initialize$lambda$8(TabPacketLogController.kt:95)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:750)
#

🤦‍♂️ nvm close this, i should add items to observable list, not to filtered one...