如何从 Thread 对象重新绘制 jTree?

发布于 2024-12-01 15:05:48 字数 3433 浏览 1 评论 0原文

我正在使用 jTree 结构来显示用户当前正在使用的项目的结构。当然,用户可以打开另一个项目,然后软件应该显示新项目的结构。我正在使用 DefaultTreeModel 添加/删除节点,并更新 jTree 结构。由于此任务可能需要几秒钟才能完成,因此它是在一个新的Thread 中实现的。代码如下:

class DrawTreeThread extends Thread {
    @Override
    public void run() {
        System.out.println("drawing the tree");
        model = (DefaultTreeModel) jTree1.getModel();
        Fichier obs,
                open,
                opens = new Fichier(Batiment.data.getAbsolutePath() + "\\Open"), 
                obss = new Fichier(Batiment.data.getAbsolutePath() + "\\Obs"), 
                rooms = new Fichier(Batiment.data.getAbsolutePath() + "\\Rooms");
        Fichier bd = new Fichier(Batiment.data.getAbsolutePath() + "\\BuildingData" + Fichier.EXT);

        int size = rooms.listFiles().length;
        int nbrEtage = bd.readInt("etage");

        DefaultMutableTreeNode[] roomz = new DefaultMutableTreeNode[size];
        DefaultMutableTreeNode bat = new DefaultMutableTreeNode(Batiment.data.getAbsoluteName());
        DefaultMutableTreeNode[] obstacles = new DefaultMutableTreeNode[size];
        DefaultMutableTreeNode[] ouvertures = new DefaultMutableTreeNode[size];
        DefaultMutableTreeNode[] etages = new DefaultMutableTreeNode[nbrEtage];
        jTree1 = new JTree(bat);

        for (int etageCounter = 0; etageCounter < nbrEtage; etageCounter++) {
            String name = "RDC";
            if (etageCounter != 0) {
                name += " + " + etageCounter;
            }
            etages[etageCounter] = new DefaultMutableTreeNode(name);
            bat.add(etages[etageCounter]);
            model.insertNodeInto(etages[etageCounter], bat, 0);
        }


        for (int i = 0; i < size; i++) {
            roomz[i] = new DefaultMutableTreeNode(rooms.listFiles()[i].getAbsoluteName());
            obs = new Fichier(obss.getAbsolutePath() + "\\" + rooms.listFiles()[i].getAbsoluteName());
            open = new Fichier(opens.getAbsolutePath() + "\\" + rooms.listFiles()[i].getAbsoluteName());
            etages[rooms.listFiles()[i].readInt("etage")].add(roomz[i]);
            obstacles[i] = new DefaultMutableTreeNode("Obstacles (" + obs.listFiles().length + ")");
            ouvertures[i] = new DefaultMutableTreeNode("Ouvertures (" + open.listFiles().length + ")");
            roomz[i].add(obstacles[i]);
            roomz[i].add(ouvertures[i]);

            DefaultMutableTreeNode[] Obs = new DefaultMutableTreeNode[obs.listFiles().length];
            for (int j = 0; j < obs.listFiles().length; j++) {
                Obs[j] = new DefaultMutableTreeNode(obs.listFiles()[j].getAbsoluteName());
                obstacles[i].add(Obs[j]);
            }

            DefaultMutableTreeNode[] Open = new DefaultMutableTreeNode[open.listFiles().length];
            for (int j = 0; j < open.listFiles().length; j++) {
                Open[j] = new DefaultMutableTreeNode(open.listFiles()[j].getAbsoluteName());
                ouvertures[i].add(Open[j]);
            }
        }

        model.nodeStructureChanged(bat);
        model.reload();

        jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        jTree1.expandRow(0);
        System.out.println("drawing the tree with success");

    }
}

从按钮 ActionListner() 调用 new DrawTreeThread().start(),但没有任何反应。怎么了?

I am using a jTree structure to display the structure of the project the user is currently using. Users can, of course, open another project, and then, the software is supposed to display the new project's structure. I am using DefaultTreeModel to add/delete nodes, and updating the jTree structure. And since this task can take seconds to be achieved, it was implemented in a new Thread. Here's the code :

class DrawTreeThread extends Thread {
    @Override
    public void run() {
        System.out.println("drawing the tree");
        model = (DefaultTreeModel) jTree1.getModel();
        Fichier obs,
                open,
                opens = new Fichier(Batiment.data.getAbsolutePath() + "\\Open"), 
                obss = new Fichier(Batiment.data.getAbsolutePath() + "\\Obs"), 
                rooms = new Fichier(Batiment.data.getAbsolutePath() + "\\Rooms");
        Fichier bd = new Fichier(Batiment.data.getAbsolutePath() + "\\BuildingData" + Fichier.EXT);

        int size = rooms.listFiles().length;
        int nbrEtage = bd.readInt("etage");

        DefaultMutableTreeNode[] roomz = new DefaultMutableTreeNode[size];
        DefaultMutableTreeNode bat = new DefaultMutableTreeNode(Batiment.data.getAbsoluteName());
        DefaultMutableTreeNode[] obstacles = new DefaultMutableTreeNode[size];
        DefaultMutableTreeNode[] ouvertures = new DefaultMutableTreeNode[size];
        DefaultMutableTreeNode[] etages = new DefaultMutableTreeNode[nbrEtage];
        jTree1 = new JTree(bat);

        for (int etageCounter = 0; etageCounter < nbrEtage; etageCounter++) {
            String name = "RDC";
            if (etageCounter != 0) {
                name += " + " + etageCounter;
            }
            etages[etageCounter] = new DefaultMutableTreeNode(name);
            bat.add(etages[etageCounter]);
            model.insertNodeInto(etages[etageCounter], bat, 0);
        }


        for (int i = 0; i < size; i++) {
            roomz[i] = new DefaultMutableTreeNode(rooms.listFiles()[i].getAbsoluteName());
            obs = new Fichier(obss.getAbsolutePath() + "\\" + rooms.listFiles()[i].getAbsoluteName());
            open = new Fichier(opens.getAbsolutePath() + "\\" + rooms.listFiles()[i].getAbsoluteName());
            etages[rooms.listFiles()[i].readInt("etage")].add(roomz[i]);
            obstacles[i] = new DefaultMutableTreeNode("Obstacles (" + obs.listFiles().length + ")");
            ouvertures[i] = new DefaultMutableTreeNode("Ouvertures (" + open.listFiles().length + ")");
            roomz[i].add(obstacles[i]);
            roomz[i].add(ouvertures[i]);

            DefaultMutableTreeNode[] Obs = new DefaultMutableTreeNode[obs.listFiles().length];
            for (int j = 0; j < obs.listFiles().length; j++) {
                Obs[j] = new DefaultMutableTreeNode(obs.listFiles()[j].getAbsoluteName());
                obstacles[i].add(Obs[j]);
            }

            DefaultMutableTreeNode[] Open = new DefaultMutableTreeNode[open.listFiles().length];
            for (int j = 0; j < open.listFiles().length; j++) {
                Open[j] = new DefaultMutableTreeNode(open.listFiles()[j].getAbsoluteName());
                ouvertures[i].add(Open[j]);
            }
        }

        model.nodeStructureChanged(bat);
        model.reload();

        jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        jTree1.expandRow(0);
        System.out.println("drawing the tree with success");

    }
}

the new DrawTreeThread().start() is called from a button ActionListner(), but nothing is happening. What's so wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2024-12-08 15:05:48
jTree1 = new JTree(bat); 

这没有任何作用。您所做的就是在内存中创建一个新的 JTree 对象。您实际上还没有将树添加到 GUI 中。

无需创建新树。相反,您可以创建一个新的 TreeModel 并使用以下方法更新现有树:

tree.setModel(...);

此外,当您调用上述方法时,您应该将其包装在 SwingUtilities.invokeLater() 中,以便在 EDT 上更新 GUI。

jTree1 = new JTree(bat); 

This does nothing. All you have done is create a new JTree object in memory. You haven't actually added the tree to the GUI.

There is no need to create a new tree. Instead you create a new TreeModel and update the existing tree by using:

tree.setModel(...);

Also, when you invoke the above method you should wrap it in a SwingUtilities.invokeLater() to the GUI is updated on the EDT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文