P R
P R

Reputation: 1301

Populating a JTree from an array of objects using NetBeans

I'm trying to populate a JTree from an array of Objects where A's ObjName is the parent and B's ObjName1 are leaf nodes.

class A {
    int a;
    String ObjName;
    B[] b = new B[10];
    A() {
        for (int i = 0; i < 10; i++) {
            b[i] = new B();
        }
    }

    class B {
        String ObjName1;
    }
}

I'm using NetBeans IDE. As of Now, I'm only able to place all the objects by hardcoding in the Properties,TreeModel dialog Box of NetBeans.

The Tree is a part of a huger application on JFrame. I've been trying to fit in the above code for two days but haven't succeeded yet. 1.In my situ, is it better to manually code it or use NetBeans? 2. Links/Eg on netbeans wold be great. 3. From what I've read, do I have to create a new data model to populate my Jtree? Thanks so much.

Upvotes: 0

Views: 805

Answers (1)

Laf
Laf

Reputation: 8185

First, you should use the code formatter to make your code readable. Right now, it's quite hard to quickly understand what it does.

For your questions:

  1. I'm a firm believer that one should code this kind of stuff manually at first, to fully understand how it works, before using any kind of generator. Whether you do it in NetBeans, Eclipse, or any other IDE, it doesn't really matter, as long as you code it manually.

  2. I've never used NetBeans, but I think you require an example if you are to code this manually. I'm not even sure that you can generate the code you need, but I might be wrong.

  3. Yes, creating a model is the first step to success in your case. Objects that display structured data (JTree or JTable for example) usually need a data model. You should look at the TreeModel class, and the Java tutorial on trees.

Upvotes: 1

Related Questions