Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:55:31+00:00 2026-05-27T09:55:31+00:00

I tried to create my own JTree Model so I can update the graphics

  • 0

I tried to create my own JTree Model so I can update the graphics by calling: updateUI();
I ran into a problem when my JTree should display a tree with a depth over 2 branches.

I use a own class that has extended by java.util.Vector called: NamedVector.

Here’s how the tree should look like:

ROOT
    Branch
        BranchInside
              Leaf
              Leaf
              ...
        OtherBranch
     AndOneMore
        ...
     ...

And here’s how it looks like: http://imageshack.us/photo/my-images/39/fehlerqq.png/

enter image description here

Here’s how I set the new TreeModel:

jTree1.setModel(new javax.swing.tree.TreeModel(){

        @Override
        public Object getRoot(){
            return core.Project.sharedInstance.getTranslationsTree();
        }

        @Override
        public Object getChild(Object parent, int index){
            if(parent instanceof String) return null;
            return ((core.NamedVector)parent).get(index);
        }

        @Override
        public int getChildCount(Object parent){
            if(parent instanceof String) return 0;
            return ((core.NamedVector)parent).size();
        }

        @Override
        public boolean isLeaf(Object node){
            if(node instanceof String) return true;
            return ((core.NamedVector)node).isEmpty();
        }

        @Override
        public int getIndexOfChild(Object parent, Object child){
            if(parent instanceof String) return -1;
            return ((core.NamedVector)parent).indexOf(child);
        }

        @Override
        public void valueForPathChanged(TreePath path, Object newValue){}
        @Override
        public void addTreeModelListener(TreeModelListener l){}
        @Override
        public void removeTreeModelListener(TreeModelListener l){}
    });

(core.Project.sharedInstance.getTranslationsTree() returns the root vector of my tree, when I print it in the console, it gives correct results.)

And here is my NamedVector class:

public class NamedVector extends java.util.Vector{
public String name;
public NamedVector(String name){
    this.name = name;
}
@Override
public String toString(){
    return name;
}
public static void dirToVector(java.io.File sourceLocation, NamedVector target){
    if(sourceLocation.isDirectory()){
        String[] children = sourceLocation.list();
        for(int i=0; i<children.length; i++){
            NamedVector vector = new NamedVector(children[i]);
            target.add(vector);
            dirToVector(new java.io.File(sourceLocation, children[i]),
                    vector);
        }
    }
}
public void print(){
    print("");
}
private void print(String pre){
    System.out.println(pre+name);
    pre+=" ";
    for(int i=0;i<this.size();i++){
        if(get(i) instanceof NamedVector){
            ((NamedVector)get(i)).print(pre);
        }else{
            System.out.println(pre+get(i).toString());
        }
    }
}}

This only happens when I use my own model on the tree, if I build it with DefaultMutableTreeNode it displays everything correct, but I would like to use a custom model.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:55:32+00:00Added an answer on May 27, 2026 at 9:55 am

    My best guess, based on your comment

    But for DefaultTreeModel i have to add my nodes manually when i change my vector, i would like only changing my vector and calling the updateUI() method so my tree is builded by his own

    is that your TreeModel does not fire the correct events. So when a change occurs in your vector, the JTree is not informed. Afterwards, when it repaints/expands a node/… and asks the TreeModel for information, this information will be out of sync with the info the JTree received through the events, typically resulting in JTrees as shown in your pictures.

    Certainly if your model has empty implementations for the addTreeModelListener method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE 3: I created a Visual Studio 2008 test project and tried to create
I tried to create my own templated helpers, but I got stuck on TextBoxFor
I tried to create my own Border class and then insert it in my
Ive tried to create my own implementation of a bignum library I cant seem
I tried to create the record in the datastore with my own key: class
I am trying to create my own scroll bars. I have tried most of
since I can not markup content inside tags I want to create my own
I tried to create a batch file that can backup all my databases. My
Ok, I have tried to create my own encryption/decryption methods using PHP mcrypt ,
How can I create my own methods which take a block as an argument

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.