TableView ChangeListener抛出IndexOutOfBoundsException

时间:2016-01-17 23:54:51

标签: java javafx tableview changelistener

我正在制作某种文件系统资源管理器,可以这么说。我有一个TableView,其中有一个TableColumnTableColum填充了当前文件夹中文件的名称。我希望能够点击TableColumn中的一个项目,如果该项目是文件夹,则移动到该文件夹​​。为此,我想,我需要一个ChangeListener。问题是,它似乎抛出了IndexOutOfBoundsException。我不知道为什么会那样做。我想我知道它发生在哪里,但我不知道为什么。

以下是相关代码:

@FXML TableView fileSystem;
private File previous = null;
private File current = null;

private void listFiles() {
    try {    
        ObservableList<MyFile> list = FXCollections.observableArrayList();
        if (current.listFiles() != null && list != null) {
            for (File i : current.listFiles()) {
                if ((i.isDirectory() || isPicture(i)) && !i.getName().equals("$Recycle.Bin"))
                        list.add(new MyFile(i));
            }
            fileSystem.setItems(list);
            path.setText(current.getAbsolutePath());
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // File System Change Listener
    fileSystem.getSelectionModel().selectedItemProperty().addListener((observableValue, oldValue, newValue) -> {
        ObservableList selectedCells = fileSystem.getSelectionModel().getSelectedCells();
        if (selectedCells != null && selectedCells.size() != 0) {
            TablePosition position = (TablePosition)selectedCells.get(0);
            if (position != null && fileSystem.getColumns() != null && fileSystem.getColumns().size() != 0) {
                int row = position.getRow();
                TableColumn tableColumn = (TableColumn)fileSystem.getColumns().get(0);
                try {
                    String fileName = (String)tableColumn.getCellData(row);

                    System.out.println(fileName);
                    String name = current.getAbsolutePath();
                    if (!name.endsWith("\\"))
                    name += "\\";
                    name += fileName;

                    File file = new File(name);
                    System.out.println("This: " + file.getAbsolutePath());
                    System.out.println("Current: " + current.getAbsolutePath());
                    if (file.isDirectory()) {
                        previous = current;
                        current = file;
                        listFiles();
                    }
                }
                catch (Exception e) {
                    //e.printStackTrace();
                }
            }
        }
    });


 }

这是例外

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 javafx.scene.control.TableView$TableViewArrayListSelectionModel.handleSelectedCellsListChangeEvent(TableView.java:2959)
    at javafx.scene.control.TableView$TableViewArrayListSelectionModel.clearAndSelect(TableView.java:2395)
    at javafx.scene.control.TableView$TableViewSelectionModel.clearAndSelect(TableView.java:1898)
    at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.simpleSelect(TableCellBehaviorBase.java:215)
    at com.sun.javafx.scene.control.behavior.TableCellBehaviorBase.doSelect(TableCellBehaviorBase.java:148)
    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.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:3758)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$304/912389815.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:927)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

问题似乎出在这一行:String fileName = (String)tableColumn.getCellData(row); 引用currentprevious已初始化,我已经检查过了。 提前谢谢。

编辑:如果你愿意,我可以发布完整的源代码,但我认为最好省略与问题无关的部分。

0 个答案:

没有答案