When setting the root of a TreeView, calling clear on it, populating it and then clearing it again, an IndexOutOfBoundsException is triggered. This was actually a logic error in my program, however it does seem this is not desired behaviour and may point to an underlying issue.
java.lang.IndexOutOfBoundsException: bitIndex < 0: -1
at java.util.BitSet.clear(Unknown Source)
at javafx.scene.control.MultipleSelectionModelBase.clearSelection(MultipleSelectionModelBase.java:411)
at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1091)
at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1044)
at com.sun.javafx.scene.control.WeakEventHandler.handle(WeakEventHandler.java:62)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:56)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:162)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:115)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:47)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.control.TreeItem.fireEvent(TreeItem.java:750)
at javafx.scene.control.TreeItem.updateChildren(TreeItem.java:842)
at javafx.scene.control.TreeItem.access$100(TreeItem.java:197)
at javafx.scene.control.TreeItem$2.onChanged(TreeItem.java:392)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:78)
at com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:117)
at hs.javafx.TreeIndexOutOfBoundsTest$1.handle(TreeIndexOutOfBoundsTest.java:42)
at hs.javafx.TreeIndexOutOfBoundsTest$1.handle(TreeIndexOutOfBoundsTest.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:56)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:162)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:115)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:47)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:2573)
at javafx.scene.Scene$ClickGenerator.access$6100(Scene.java:2507)
at javafx.scene.Scene$MouseHandler.process(Scene.java:2790)
at javafx.scene.Scene$MouseHandler.process(Scene.java:2606)
at javafx.scene.Scene$MouseHandler.access$1300(Scene.java:2579)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1252)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:1857)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:203)
at com.sun.glass.ui.View.handleMouseEvent(View.java:288)
at com.sun.glass.ui.View.notifyMouse(View.java:548)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
at java.lang.Thread.run(Unknown Source)
Run the provided program, and double click the indicated item -- you will get a IndexOutOfBoundsException.
Also seeRT-15024 for a similar issue.
Program:
public class TreeIndexOutOfBoundsTest extends Application {
public static void main(String[] args) {
Application.launch(TreeIndexOutOfBoundsTest.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
StackPane stackPane = new StackPane();
final TreeView<Label> treeView = new TreeView<>();
stackPane.getChildren().setAll(treeView);
treeView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
TreeItem<Label> focusedItem = treeView.getFocusModel().getFocusedItem();
if(focusedItem != null) {
if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
TreeItem<Label> root = new TreeItem<>(new Label("Something else"));
treeView.setRoot(root);
treeView.getRoot().getChildren().clear();
TreeItem<Label> item = new TreeItem<>(new Label("New Thing"));
root.getChildren().add(item);
TreeItem<Label> item2 = new TreeItem<>(new Label("New Thing 2"));
root.getChildren().add(item2);
treeView.getRoot().getChildren().clear();
TreeItem<Label> item3 = new TreeItem<>(new Label("New Thing"));
root.getChildren().add(item3);
TreeItem<Label> item4 = new TreeItem<>(new Label("New Thing 2"));
root.getChildren().add(item4);
}
}
}
});
primaryStage.setScene(new Scene(stackPane));
primaryStage.setHeight(100);
primaryStage.setWidth(100);
primaryStage.show();
TreeItem<Label> root = new TreeItem<>(new Label("Library Panel"));
treeView.setEditable(false);
treeView.setShowRoot(false);
treeView.setRoot(root);
root.setExpanded(true);
TreeItem<Label> item = new TreeItem<>(new Label("Thing"));
root.getChildren().add(item);
TreeItem<Label> item2 = new TreeItem<>(new Label("Double click me!"));
root.getChildren().add(item2);
}
}
java.lang.IndexOutOfBoundsException: bitIndex < 0: -1
at java.util.BitSet.clear(Unknown Source)
at javafx.scene.control.MultipleSelectionModelBase.clearSelection(MultipleSelectionModelBase.java:411)
at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1091)
at javafx.scene.control.TreeView$TreeViewBitSetSelectionModel$2.handle(TreeView.java:1044)
at com.sun.javafx.scene.control.WeakEventHandler.handle(WeakEventHandler.java:62)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:56)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:162)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:115)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:47)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.control.TreeItem.fireEvent(TreeItem.java:750)
at javafx.scene.control.TreeItem.updateChildren(TreeItem.java:842)
at javafx.scene.control.TreeItem.access$100(TreeItem.java:197)
at javafx.scene.control.TreeItem$2.onChanged(TreeItem.java:392)
at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:78)
at com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:117)
at hs.javafx.TreeIndexOutOfBoundsTest$1.handle(TreeIndexOutOfBoundsTest.java:42)
at hs.javafx.TreeIndexOutOfBoundsTest$1.handle(TreeIndexOutOfBoundsTest.java:1)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:56)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:162)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:115)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:47)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:2573)
at javafx.scene.Scene$ClickGenerator.access$6100(Scene.java:2507)
at javafx.scene.Scene$MouseHandler.process(Scene.java:2790)
at javafx.scene.Scene$MouseHandler.process(Scene.java:2606)
at javafx.scene.Scene$MouseHandler.access$1300(Scene.java:2579)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1252)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:1857)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:203)
at com.sun.glass.ui.View.handleMouseEvent(View.java:288)
at com.sun.glass.ui.View.notifyMouse(View.java:548)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
at java.lang.Thread.run(Unknown Source)
Run the provided program, and double click the indicated item -- you will get a IndexOutOfBoundsException.
Also see
Program:
public class TreeIndexOutOfBoundsTest extends Application {
public static void main(String[] args) {
Application.launch(TreeIndexOutOfBoundsTest.class, args);
}
@Override
public void start(Stage primaryStage) throws Exception {
StackPane stackPane = new StackPane();
final TreeView<Label> treeView = new TreeView<>();
stackPane.getChildren().setAll(treeView);
treeView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
TreeItem<Label> focusedItem = treeView.getFocusModel().getFocusedItem();
if(focusedItem != null) {
if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() == 2) {
TreeItem<Label> root = new TreeItem<>(new Label("Something else"));
treeView.setRoot(root);
treeView.getRoot().getChildren().clear();
TreeItem<Label> item = new TreeItem<>(new Label("New Thing"));
root.getChildren().add(item);
TreeItem<Label> item2 = new TreeItem<>(new Label("New Thing 2"));
root.getChildren().add(item2);
treeView.getRoot().getChildren().clear();
TreeItem<Label> item3 = new TreeItem<>(new Label("New Thing"));
root.getChildren().add(item3);
TreeItem<Label> item4 = new TreeItem<>(new Label("New Thing 2"));
root.getChildren().add(item4);
}
}
}
});
primaryStage.setScene(new Scene(stackPane));
primaryStage.setHeight(100);
primaryStage.setWidth(100);
primaryStage.show();
TreeItem<Label> root = new TreeItem<>(new Label("Library Panel"));
treeView.setEditable(false);
treeView.setShowRoot(false);
treeView.setRoot(root);
root.setExpanded(true);
TreeItem<Label> item = new TreeItem<>(new Label("Thing"));
root.getChildren().add(item);
TreeItem<Label> item2 = new TreeItem<>(new Label("Double click me!"));
root.getChildren().add(item2);
}
}