Domanda

I have a TreeView that I've modified by putting an HBox in the "graphic" of the TreeItem label. This HBox contains a MenuButton. I want to be able to automatically select the TreeItem whenever focus is given to the MenuButton inside it -- something JavaFX doesn't do automatically. However, when I do something like this in the TreeView's cell factory:

menuButton.focusedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> value, Boolean oldVal, Boolean newVal)
{
    if(newVal.equals(oldVal))
        return;

    if(newVal)
    {
        TreeItem<FormationDataModel> treeItem = getTreeItem();
        System.out.println("Setting selection to " + treeItem + "...");
        treeView.getSelectionModel().select(treeItem);
    }
}});

The focus on the menu button will often cause this Exception:

java.lang.NullPointerException
at javafx.scene.Scene$ScenePulseListener.synchronizeSceneProperties(Scene.java:2148)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2210)
at com.sun.javafx.tk.Toolkit$5.run(Toolkit.java:363)
at com.sun.javafx.tk.Toolkit$5.run(Toolkit.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:361)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:384)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:463)
at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:332)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)

Does anyone have any idea what would cause this or how to fix it so that I can have a TreeItem selected automatically when a node inside the TreeItem gains focus?

È stato utile?

Soluzione

For anyone else interested, this turns out to be one or more bugs in JavaFX 2.2 specifically where they override the implementation of the MultipleSelectionModel to fix OTHER bugs. In the process of doing that, they cause all sorts of problems because they automatically expand the children of any selected item. When you do that, many of the TreeItems are invalidated, causing all sorts of sync havoc. The workaround was to select items by INDEX instead of by OBJECT. That works perfectly fine.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top