Jtree and Netbeans

Let me start off by saying that the Jtree in Netbeans may very well be the scourge of my existence. And nothing about netbeans pains me more than the default UI developer, which uses some kind of awkward position scheme to make sure that the display matches across all systems, and turning it off, while it may relieve the initial stress of having to work around this arbitrary [and I’m sure purposeful] placement methodology, where if you move one thing, 6 things decide to come tumbling with it. I’ll be up front and admit while I understand the basics of coding [File /  Database IO , Loops, etc.] that I am by no means an expert in any particular language, and I’m sure there are still some concepts out there that I haven’t stumbled into yet or just haven’t worked with in-depth. But recently I got the joy of working with the Netbeans IDE and developing an interface that required the Jtree element. Upon seeing it, it’s a simple kind of File tree looking thing with a root / categories / items in those categories. It works on the exterior on a very basic and generally intuitive interface. However, the code that it actually develops on the back-end was enough to make my eyes bleed. The thing about netbeans is that it’s actually pretty good about taking care of the code for the GUI functions, so it’s really easy to work with, but that’s where this little joke ends.

I want to note that before I wrote this, I spent probably a good 3 hours Sunday just trying to play around with the very basic structure of the Jtree so that I could manipulate it. Basically the idea was that I wanted to update it dynamically. The solution I came up with is probably a tad bit ghetto, but for my purposes it works. The first thing you have to get your head around is that they really do you want you to use the Model function, in Netbeans itself, it won’t actually place the code for the GUI control until you right-click the element, select properties, and then choose to select the Tree Model, it will take you to a decent GUI based method of actually setting up how you want this thing to start off. The problem of course being up until that point there was no code element to work with prior to you establishing that, or if there was, my friend and I certainly missed it on the first few passes. This of course leads us to actually wanting to change the tree while the program is in action. The problem with this is that the items they set up for the root are in init_components and they just happen to be private, and while I quite like them like that, I really would rather have my own thing to work with.

This is where we get into the actual “Tree Model” that everyone is so happy to work with [why they put up with this I do not know]. Anyway it works on a very basic principle. You define a Node with

javax.swing.tree.DefaultMutableTreeNode nameOfNode = new javax.swing.tree.DefaultMutableTreeNode(“stringDisplayedOnScreen”);

Now if you are feeling really special, you can probably get away with simply doing an import of javax.swing.tree, because apparently netbeans doesn’t get the initiative to add it all on its own when you first set up the Jtree, so if you want to just shorten that code down to the DefaultMutableTreeNode [I know there has to be a logical reason for creating such a long and boring class but as of yet, I am not sure what it is]. This of course comes with a few methods, but the only one I really care about is the “add” method that allows you start actually building your tree. So what we’re going to do is actually create another node

DefaultMutableTreeNode nameOfSubNode1 = new DefualtMutableTreeNode(“stringOfSubNodeDisplayedOnScreen”);

Really, it’s quite easy, or rather it should be. The key to understand here, is that the nameOfNode is pretty much the basis of your tree. Netbeans / Java refer to this as the root node, and I want to make it clear, that when you see all these tutorials out there about “creating the model” all they really mean is shove your root node into that model. The problem of course is that again, the original root they constructed isn’t actually accessible. So what was my “Ghetto Solution” well, I’m just going to rebuild it from scratch. I’ve got a method called save buried somewhere in this program that I created that saves all these attributes from various text files to an array list. And basically what I’m going to do is concatenate the ID field and First and Last Name field and just assign it a string value [I’m sure you can do fancier things, but this is just me playing with the basics]. Because the tree I was constructing was going to be relatively simple, I get to make a few very nice judgment calls

The basic structure of the tree on-screen was as follows

–Graduate 1 (representing some students)
–Graduate 2

So on and so forth, the basic idea was I was going to add to the list whenever a new graduate, undergrad, whatever was added to the list, So to do this in the save function I actually just rebuilt the basic structure of the tree and added all the concatenated string based off going through the entire array list [I mentioned this was a ghetto method correct?] So that the tree gets rebuilt each time it’s saved. essentially calling the following function every time I happened to save something.

private void treeModel(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(“Students”);
DefaultMutableTreeNode treeUndergraduate = new DefaultMutableTreeNode(“Undergraduate”);
DefaultMutableTreeNode treeGraduate = new DefaultMutableTreeNode(“Graduate”);
DefaultMutableTreeNode treeNonDegree = new DefaultMutableTreeNode(“NonDegree”);
String treeItem = “”;
DefaultMutableTreeNode treeAdd = null;
//Add Based On the array
for(int i = 0; i < storage.size(); i++){
if(storage.get(i) instanceof Graduate){
Graduate temp = (Graduate) storage.get(i);
treeItem = temp.PUID() + “:” + temp.firstName() + ” ” + temp.lastName();
treeAdd = new DefaultMutableTreeNode(treeItem);
else if(storage.get(i) instanceof NonDegree){
NonDegree temp = (NonDegree) storage.get(i);
treeItem = temp.PUID() + “:” + temp.firstName() + ” ” + temp.lastName();
treeAdd = new DefaultMutableTreeNode(treeItem);
Undergraduate temp = (Undergraduate) storage.get(i);
treeItem = temp.PUID() + “:” + temp.firstName() + ” ” + temp.lastName();
treeAdd = new DefaultMutableTreeNode(treeItem);
//Add the Tree Back to the model
studentTree.setModel(new javax.swing.tree.DefaultTreeModel(root));

Now the real basics of this code are I’m taking that tree model, building the basics, and then adding students based on whatever happens to be in the Array. And studentTree of course happens to be the name of my UI Jtree. This rebuilds the tree, fills it with the original content, and then adds all the items in the array based on what they of class they are. It’s nifty if you are trying to just do a Basic Jtree, I’m sure it can do fancier things, but for right now that’s all I needed it to do.

I also needed the Jtree to have an effect on another UI element whenever someone happened to click a student in that list. Luckily for this, all we needed to do was go to the actual UI element in the design screen, right-click, go to our lovely list of actions and choose TreeSelect > ValueChanged, basically it finds out what has been clicked and you can get a hold of the name of that particular item because it comes with a nifty property.

DefaultMutableTreeNode node = (DefaultMutableTreeNode) studentTree.getLastSelectedPathComponent();
//Gets the node that’s currently selected  in our Jtree
Object nodeInfo = node.getUserObject();

And that getUserObject will find out the actual string value that’s been assigned to that node. And I don’t know how helpful this was to you guys, but I know when I was trying to search for things on the Jtree it all came back pretty sparse or were for people who were used to doing the Java GUI by hand, so I hope that this helped someone out there.

This entry was posted in Code and tagged , , . Bookmark the permalink.

One Response to Jtree and Netbeans

  1. Tim says:

    Great Hints…… I agree very little publish info on how to use this control from a GUI (Netbeans in my case)…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s