0

我试图在创建 JTree 时选择它的第一行。但有时我会遇到以下异常:

java.lang.NullPointerException
   at javax.swing.plaf.basic.BasicTreeUI.completeEditing(BasicTreeUI.java:2035)
   at javax.swing.plaf.basic.BasicTreeUI$Handler.valueChanged(BasicTreeUI.java:3715)
   at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:634)
   at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1092)
   at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:293)
   at javax.swing.JTree.setSelectionPaths(JTree.java:1644)
   at javax.swing.JTree.setSelectionRows(JTree.java:1721)
   at javax.swing.JTree.setSelectionRow(JTree.java:1696)

在调用 tree.setSelectionRow(0) 之前,我尝试进行以下检查,但没有任何效果:

    while((!tabTree.isVisible()) || (tabTree.isEditing()) || (tabTree.getRowCount() == 0)) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    tabTree.setSelectionRow(0);

你需要被拯救:)

有什么建议么?

4

1 回答 1

3

这可能是由以下原因引起的:

  • 您正在使用返回的自定义单元格编辑nullgetTreeCellEditorComponent()
  • 您在非 UI 线程中修改树。请参阅Swing 中的并发
于 2013-01-30T13:26:04.560 回答