-1

I have created a listview which is populated when a textfield field is focused. Similar question has been asked before at IndexOutOfBoundsException while updating a ListView in JavaFX . But i am not getting a solution. Youtube video showing the error.

ObservableList<String> productsList = FXCollections.observableArrayList();
ObservableList<String> partialList = FXCollections.observableArrayList();
fx_product_name_tb.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            if(newValue){
                fx_pane_list_view.setVisible(true);
                fx_products_list_view.getItems().setAll(productsList);
            }else{
                fx_pane_list_view.setVisible(false);
            }
        }
    });

When i enter character in textfield the list is sorted according to it using textproperty listener.

fx_product_name_tb.textProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            String partialName = fx_product_name_tb.getText();
            System.out.println("check : "+partialName);
            if(!partialName.equals("")){
                try {
                    partialList = searchName(productsList, partialName);
                }catch(IndexOutOfBoundsException ex){
                    System.out.println(ex.getMessage());
                }catch (NullPointerException ex){
                    System.out.println(ex.getMessage());
                }
                fx_products_list_view.getItems().setAll(partialList);//i am getting IndexOfBoundException here
            }


        }
    }); 

Sorting function is

public static  ObservableList<String> searchName(ObservableList<String> list, String string){
    ObservableList<String> sub = FXCollections.observableArrayList();
    int size;
    for(String s : list){
        if(s.startsWith(string)){
            sub.add(s);
        }
    }
    return sub;
}

When i select a value from it is shown in textfield with

fx_products_list_view.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(
                        ObservableValue<? extends String> observable,
                        String oldValue, String newValue) {
                    fx_product_name_tb.setText(newValue);

                }
            });

The IndexOutOfBoundException that i am getting is.

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:136)
at javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:242)
at com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new$177(ListViewBehavior.java:269)
at javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
at com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:75)
at javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:378)
at javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1403)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:256)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:220)
at com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

Please look ino the problem give a solution. Youtube video showing the error.

Ranjit Vamadevan
  • 514
  • 5
  • 21

1 Answers1

0

The update to the TextField should be wrapped into Platform.runLater() in order to avoid this exception. As already mentioned by Slaw, GUI updates must be run in JavaFX thread.

fx_products_list_view.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
                @Override
                public void changed(
                        ObservableValue<? extends String> observable,
                        String oldValue, String newValue) {
                    Platform.runLater(()->fx_product_name_tb.setText(newValue));

                }
            });
  • Okay, I see. One possibility to get around is, when `partialList` is technically a `FilteredList` and the filter criterion is updated when a text is typed into `TextField`. Using bindings this can achieved in a way, so that no Exceptions shall occur. –  Mar 14 '19 at 21:02
  • 1
    Please try out following example, I just dont know yet if it has the behavior you expect. One approach: https://gist.github.com/raumzeitfalle/5339d3966e42d8f0ade7c44f655d2744 , An alternative approach: https://gist.github.com/raumzeitfalle/fab786088b45e1fcc25526938f3e8694 –  Mar 14 '19 at 22:21
  • @RanjitVamadevan This will only help if the `fx_products_list` isn't attached to a live scene graph while `fx_product_name_tb` is, but I'm guessing both objects are live. The stack trace you posted indicates some background thread is _updating the `ListView`_ in some way. I suspect this because the error only shows JavaFX classes in the trace—which means there's either a bug in the library (unlikely) or there's threading issues in your code (more likely). Modifying any live GUI object from a background thread can lead to undefined behavior, such as an `IndexOutOfBoundsException`. – Slaw Mar 15 '19 at 07:04