Running the following code throws the IndexOutOfBoundsException shown below:
public class ChoiceBoxSelect extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox vbox = new VBox(10);
final ChoiceBox<String> cb = new ChoiceBox<String>();
Button b = new Button("Populate ChoiceBox");
b.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
ObservableList<String> items = FXCollections.observableArrayList("one", "two", "three");
cb.setItems(items);
cb.getSelectionModel().select(0);
}
});
vbox.getChildren().addAll(cb, b);
Scene s = new Scene(vbox, 300, 400);
stage.setScene(s);
stage.show();
}
public static void main(String[] args) {
ChoiceBoxSelect.launch(args);
}
}
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:141)
at com.sun.javafx.scene.control.skin.ChoiceBoxSkin.handleControlPropertyChanged(ChoiceBoxSkin.java:171)
at com.sun.javafx.scene.control.skin.SkinBase$4.changed(SkinBase.java:242)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:67)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:64)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:154)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:120)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:89)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:122)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:81)
at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:181)
at javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:118)
at javafx.scene.control.ChoiceBox$ChoiceBoxSelectionModel.select(ChoiceBox.java:291)
at controls.ChoiceBoxSelect$1.handle(ChoiceBoxSelect.java:34)
at controls.ChoiceBoxSelect$1.handle(ChoiceBoxSelect.java:29)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:60)
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:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:5883)
at javafx.scene.control.Button.fire(Button.java:158)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:169)
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(SkinBase.java:297)
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(SkinBase.java:290)
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.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$MouseHandler.process(Scene.java:2702)
at javafx.scene.Scene$MouseHandler.process(Scene.java:2523)
at javafx.scene.Scene$MouseHandler.access$1200(Scene.java:2496)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1229)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:1813)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:200)
at com.sun.glass.ui.View.handleMouseEvent(View.java:256)
at com.sun.glass.ui.View.notifyMouse(View.java:516)
public class ChoiceBoxSelect extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox vbox = new VBox(10);
final ChoiceBox<String> cb = new ChoiceBox<String>();
Button b = new Button("Populate ChoiceBox");
b.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent actionEvent) {
ObservableList<String> items = FXCollections.observableArrayList("one", "two", "three");
cb.setItems(items);
cb.getSelectionModel().select(0);
}
});
vbox.getChildren().addAll(cb, b);
Scene s = new Scene(vbox, 300, 400);
stage.setScene(s);
stage.show();
}
public static void main(String[] args) {
ChoiceBoxSelect.launch(args);
}
}
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:141)
at com.sun.javafx.scene.control.skin.ChoiceBoxSkin.handleControlPropertyChanged(ChoiceBoxSkin.java:171)
at com.sun.javafx.scene.control.skin.SkinBase$4.changed(SkinBase.java:242)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:67)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:181)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:64)
at javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:154)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:120)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:89)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:122)
at javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:81)
at javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:181)
at javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:118)
at javafx.scene.control.ChoiceBox$ChoiceBoxSelectionModel.select(ChoiceBox.java:291)
at controls.ChoiceBoxSelect$1.handle(ChoiceBoxSelect.java:34)
at controls.ChoiceBoxSelect$1.handle(ChoiceBoxSelect.java:29)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:60)
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:28)
at javafx.event.Event.fireEvent(Event.java:171)
at javafx.scene.Node.fireEvent(Node.java:5883)
at javafx.scene.control.Button.fire(Button.java:158)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:169)
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(SkinBase.java:297)
at com.sun.javafx.scene.control.skin.SkinBase$5.handle(SkinBase.java:290)
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.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$MouseHandler.process(Scene.java:2702)
at javafx.scene.Scene$MouseHandler.process(Scene.java:2523)
at javafx.scene.Scene$MouseHandler.access$1200(Scene.java:2496)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1229)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:1813)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:200)
at com.sun.glass.ui.View.handleMouseEvent(View.java:256)
at com.sun.glass.ui.View.notifyMouse(View.java:516)