尝试在JTree中使用setSelectionPath时java.util.ConcurrentModificationException

时间:2012-06-09 15:46:31

标签: java swing exception jtree

点击按钮后,我一直在尝试在JTree中设置选择路径。代码正确设置路径,但每次单击它我都会得到一个

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at com.treeview.main.iui.SelectableTree.actionPerformed(SelectableTree.java:225)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

我不知道是什么导致它。我读到当多个线程尝试访问相同的资源但我看不到问题时会发生这种情况。

以下是我正在尝试运行的代码:

/**
     * Handles the event when a button is clicked.
     */
    @Override
    public void actionPerformed(ActionEvent arg0) {
            // If button "Force adaptation is clicked"
            if (arg0.getSource() == forceAdapt) {
                    // ModelContainer.getInstance().getAdaptedTree().removeAllChildren();
                    // tree.setModel(new DefaultTreeModel(ModelContainer.getInstance()
                    // .getAdaptedTree()));
                    // treeModel.reload();
            } else if (arg0.getSource() == showAll) {
                    expandFullTree(tree, ModelContainer.getInstance().getAdaptedTree());
                    // If button "Show All" is clicked
                    // tree.setModel(new DefaultTreeModel(ModelContainer.getInstance()
                    // .getFullTree()));
                    // treeModel.reload();
            }
            // In case recommendation button was clicked.

            for (JButton button : recommendButtons) {
                    if (arg0.getSource() == button) {
                            TreeHandler th = new TreeHandler();
                            TreeItem node = th.getNodeByName(ModelContainer.getInstance()
                                            .getAdaptedTree(), button.getText());

                            tree.setSelectionPath(new TreePath(node.getPath()));
                    }
            }

    }

这两个方法用按钮填充ArrayList:

 /**
 * Handles tree selection event.
 */
public void valueChanged(TreeSelectionEvent event) {
    selectedNode = (TreeItem) tree.getLastSelectedPathComponent();

    // If there's predecessors.
    if (!lastSelectedNode.getId().equals("-1")) {
        // Display selection in bottom bar.
        currentSelectionField.setText("Current Selection: "
                + selectedNode.toString() + " Prev Node : "
                + lastSelectedNode.toString());
        /*
         * Increase the clicks in the transition matrix and updates the
         * probability transition matrix.
         */
        ModelContainer.getInstance().increaseClicksOnElement(
                Integer.parseInt(selectedNode.getId()) - 1,
                Integer.parseInt(lastSelectedNode.getId()) - 1);
        // Recalculate the Markovian matrix.
        ModelContainer.getInstance().recalculateMarkovian();

        // Display recommendations
        displayRecommendations(ModelContainer.getInstance()
                .getNextMarkovians(
                        (Integer.parseInt(selectedNode.getId()) - 1) + "",
                        5));

    }

    lastSelectedNode = selectedNode;

}

public void displayRecommendations(ArrayList<TreeItem> recommend) {

    recommendation.removeAll();

    int place = 2;
    for (TreeItem treeItem : recommend) {
        JButton aButton = new JButton(treeItem.toString());
        aButton.setPreferredSize(new Dimension(recommendation.getWidth(),
                recommendation.getHeight() / place));
        aButton.addActionListener(this);
        recommendButtons.add(aButton);
        recommendation.add(aButton);
        System.out.println(aButton.getText());
        place = place * 2;
    }
    recommendation.revalidate();
    recommendation.repaint();

}

0 个答案:

没有答案