gpt4 book ai didi

java - JTree:不会重新显示新模型

转载 作者:行者123 更新时间:2023-11-30 07:24:17 26 4
gpt4 key购买 nike

当我启动我的 Java swing 应用程序时,我将 XML 文件读入 DefaultTreeModel,使用该模型创建一个新的 JTree,将 jtree 添加到 JScrollPane,然后 Life is Good。一切正常:

// Program initialization:
jarFilesDB = new JarFilesDB ();
jarFilesDB.load(JarFilesDB.JAR_DB_FILENAME);
jtree = new JTree(jarFilesDB.getRootNode ());
JScrollPane scrollPane = new JScrollPane (jtree);
contentPane.add(scrollPane, BorderLayout.CENTER);

稍后,我可能想要创建一个 XML 文件,并完全重建我的模型。当我这样做时,我无法让 jtree 重新显示新模型。要么我得到一个空白 Pane ,要么旧模型仍然显示:

// Completely regenerate XML file and Model
jarFilesDB.mkXmlFile (sPath); // Generate new XML file
jarFilesDB.load(JarFilesDB.JAR_DB_FILENAME); // Generate new Model

根据 Andrew Thompson 的要求,我写了一个“简短(尽可能)、独立、正确(不!否则我不会问这个问题)示例):

package com.rgb;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.*;

public class FrmMain extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrmMain frame = new FrmMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FrmMain() {
setTitle("HelloJTree3");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JButton btnNewButton = new JButton("Click Me");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doClick ();
}
});
contentPane.add(btnNewButton, BorderLayout.NORTH);
jarFilesDB = new JarFilesDB ();
jarFilesDB.load(isAlpha);
isAlpha = !isAlpha;
jtree = new JTree(jarFilesDB.getRootNode ());
JScrollPane scrollPane = new JScrollPane (jtree);
contentPane.add(scrollPane, BorderLayout.CENTER);
}

private void doClick () {
jarFilesDB.load(isAlpha);
isAlpha = !isAlpha;
}

private JarFilesDB jarFilesDB;
private JTree jtree;
private boolean isAlpha = false;
}

class JarFilesDB extends DefaultTreeModel {

public JarFilesDB () {
super (null);
}

public void load (boolean isAlpha) {
if (rootNode == null)
rootNode = new DefaultMutableTreeNode("root");
else
rootNode.removeAllChildren ();

String[] values;
if (isAlpha)
values = new String[] {"A", "A1", "A2", "B", "B1", "B2"};
else
values = new String[] {"10", "11", "12", "20", "21", "22"};

DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (values[0]);
DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode (values[1]);
childNode.add(leafNode);
leafNode = new DefaultMutableTreeNode (values[2]);
childNode.add(leafNode);
rootNode.add(childNode);

childNode = new DefaultMutableTreeNode (values[3]);
leafNode = new DefaultMutableTreeNode (values[4]);
childNode.add(leafNode);
leafNode = new DefaultMutableTreeNode (values[5]);
childNode.add(leafNode);
rootNode.add(childNode);
}

public TreeNode getRootNode () {
return rootNode;
}

private DefaultMutableTreeNode rootNode = null;

}

最佳答案

add() 方法不会触发更新。最好使用 insertNodeInto。要修复,请添加以下内容:

nodeStructureChanged(rootNode);

load() 方法的末尾。

并使用它来初始化树:

jtree = new JTree(jarFilesDB);

这是更新后的 SSCCE,不确定 rootNode 的目的是什么,所以我没有管它:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTree;
import javax.swing.JScrollPane;
import javax.swing.tree.*;

public class FrmMain extends JFrame {

private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrmMain frame = new FrmMain();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public FrmMain() {
setTitle("HelloJTree3");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);

JButton btnNewButton = new JButton("Click Me");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
doClick ();
}
});
contentPane.add(btnNewButton, BorderLayout.NORTH);
jarFilesDB = new JarFilesDB ();
jarFilesDB.load(isAlpha);
isAlpha = !isAlpha;
//jtree = new JTree(jarFilesDB.getRootNode ());
jtree = new JTree(jarFilesDB);
JScrollPane scrollPane = new JScrollPane (jtree);
contentPane.add(scrollPane, BorderLayout.CENTER);
}

private void doClick () {
jarFilesDB.load(isAlpha);
isAlpha = !isAlpha;
}

private JarFilesDB jarFilesDB;
private JTree jtree;
private boolean isAlpha = false;
}

class JarFilesDB extends DefaultTreeModel {

public JarFilesDB () {
super (new DefaultMutableTreeNode("root"));
rootNode = (DefaultMutableTreeNode) getRoot();
}

public void load (boolean isAlpha) {
rootNode.removeAllChildren ();

String[] values;
if (isAlpha)
values = new String[] {"A", "A1", "A2", "B", "B1", "B2"};
else
values = new String[] {"10", "11", "12", "20", "21", "22"};

DefaultMutableTreeNode childNode = new DefaultMutableTreeNode (values[0]);
DefaultMutableTreeNode leafNode = new DefaultMutableTreeNode (values[1]);
childNode.add(leafNode);
leafNode = new DefaultMutableTreeNode (values[2]);
childNode.add(leafNode);

rootNode.add(childNode);

childNode = new DefaultMutableTreeNode (values[3]);
leafNode = new DefaultMutableTreeNode (values[4]);
childNode.add(leafNode);
leafNode = new DefaultMutableTreeNode (values[5]);
childNode.add(leafNode);
rootNode.add(childNode);

nodeStructureChanged(rootNode);
}

public TreeNode getRootNode () {
return rootNode;
}

private DefaultMutableTreeNode rootNode = null;

}

关于java - JTree:不会重新显示新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com