开发者

JTree refreshing after setting new jtree model

开发者 https://www.devze.com 2023-02-18 09:28 出处:网络
I\'m trying to use dynamically JTree component. Under root node I have four nodes and one of them (\"Operations\") can have 0 to many children. This is set by user via editable list in separate window

I'm trying to use dynamically JTree component. Under root node I have four nodes and one of them ("Operations") can have 0 to many children. This is set by user via editable list in separate window opened on users request. After editing this list user hits button 'Save' and then magic should happen. Edited list is sent to server(on the same machine actually, so it doesn't take to long), the window with list is closed but right before that the main window (with jtree) is told to refresh itself, and I can see that it does what is told in log output, but the changes don't show on the screen.

I am using DefaultTreeModel, this method is called to create model at the beginning(when first opening the window) and after the change to update the new model with new structure. with dmtn.getLeafCount() I can see that newly downloaded structure from server is the right one with the changed number of leaves under 'Operations'

public DefaultTreeModel getDataStructure() {
    int dataID = task.getData().getId();
    LoggerUtility.logger.info("Data ID: " + dataID);
    DefaultMutableTreeNode dmtn = Manager.manager.getDataStructure(task.getId());
    LoggerUtility.logger.info("DTMN created "+dmtn.getLeafCount());

    return new DefaultTreeModel(dmtn);
}

the method used to refresh the jtree looks like this (it's very messy):

public void updateTree(){
    taskDataTree.setModel(getDataStructure());
    ((DefaultTreeModel)taskDataTree.getModel()).reload();
    this.revalidate();
    this.repaint();
    taskDataTree.revalidate();
    taskDataTree.repaint();
    taskDataTree.updateUI();
    taskDataTree.setVisible(false);
    taskDataTree.setVisible(true);
    jScrollPane2.setViewportView(taskDataTree);
}

It's very messy because I have tried to put in there every possible solution to my problem that I have found on forums, I also tried with my own treemodel implementation which would call fireTreeStructureChanged(...) but it also didn't change.

I should probably also add that I'm using Netbeans GUI Builder to build my gui although I don't know if it has anything to do with that.

I would be very grateful for any help with that

BR Lucja

EDIT!!! I also tried puting it in another thread like that:

public void updateTree() {

    SwingWorker sw = new SwingWorker() {

        @Override
        protected Object doInBackground() throws Exception {
            taskDataTree.setModel(getDataStructure());
            ((DefaultTreeModel) taskDataTree.getModel()).reload();
            taskDataTree.revalidate();
            taskDataTree.repaint();
            taskDataTree.updateUI();
            taskDataTree.setVisible(false);
            taskDataTree.setVisible(true);
            jScrollPane2.setViewportView(taskDataTree);
            return null;
        }
    };
    sw.execute();
}

but it also didn开发者_C百科't help.


tree.setModel( anotherModel );

Is the only line of code that you need.

If it doesn't work then it means that the tree variable does not contain a reference to the tree that was added to the GUI. Maybe you have a class variable and a local variable of the same name.


From my point of view the own TreeModel implementation was a good approach. But I know that creating an TreeModelEvent with the correct data isn't that simple.

I would suggest to update your question with your TreeModel implementation so that we can find the problem with it.


In principle it should work this way (when you set a new Model, the tree reloads itself). (This is not the most efficient way, better let the model send appropriate events when it changes.)

If this does not work, make sure you are calling the setModel method in the AWT Event Dispatch Thread (with EventQueue.invokeLater (or SwingUtilities.invokeLater, which is the same), for example. I think you should not need all your revalidate(), repaint() etc. calls (and updateUI should only be done if you changed the look-and-feel configuration).

0

精彩评论

暂无评论...
验证码 换一张
取 消