我试图在创建 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);
你需要被拯救:)
有什么建议么?