Geertjan is a DZone Zone Leader and has posted 466 posts at DZone. You can read more from them at their website. View Full User Profile

Taking the New Swing Tree Table for a Spin

06.04.2008
| 70468 views |
  • submit to reddit
Announcing the new Swing Tree Table yesterday, Tim Boudreau writes:

Usage is incredibly easy - you just provide a standard Swing TreeModel of whatever sort you like, and an additional RowModel that can be queried for the other columns contents, editability and so forth.

I found an example from some time ago, by Tim, and have been playing with it to get used to this new development. The result is as follows:

To get started, I simply download the latest NetBeans IDE development build from netbeans.org and then attached the platform8/org-netbeans-swing-outline.jar to my Java SE project. For the rest, I wasn't required to do anything with NetBeans, necessarily. I could have attached the JAR to a project in Eclipse or anywhere else. Then I created a JFrame.

To work with this Swing tree table, you need to provide the new "org.netbeans.swing.outline.Outline" class with the new "org.netbeans.swing.outline.OutlineModel" which, in turn, is built from a plain old javax.swing.tree.TreeModel, together with the new "org.netbeans.swing.outline.RowModel". Optionally, to change the default rendering, you can use the new "org.netbeans.swing.outline.RenderDataProvider".

Let's first create a TreeModel for accessing files on disk. We will receive the root of the file system as a starting point:

private static class FileTreeModel implements TreeModel {

private File root;

public FileTreeModel(File root) {
this.root = root;
}

@Override
public void addTreeModelListener(javax.swing.event.TreeModelListener l) {
//do nothing
}

@Override
public Object getChild(Object parent, int index) {
File f = (File) parent;
return f.listFiles()[index];
}

@Override
public int getChildCount(Object parent) {
File f = (File) parent;
if (!f.isDirectory()) {
return 0;
} else {
return f.list().length;
}
}

@Override
public int getIndexOfChild(Object parent, Object child) {
File par = (File) parent;
File ch = (File) child;
return Arrays.asList(par.listFiles()).indexOf(ch);
}

@Override
public Object getRoot() {
return root;
}

@Override
public boolean isLeaf(Object node) {
File f = (File) node;
return !f.isDirectory();
}

@Override
public void removeTreeModelListener(javax.swing.event.TreeModelListener l) {
//do nothing
}

@Override
public void valueForPathChanged(javax.swing.tree.TreePath path, Object newValue) {
//do nothing
}

}

The above could simply be set as a JTree's model and then you'd have a plain old standard JTree. It would work, no problems, it would be a normal JTree. However, it wouldn't be a tree table since you'd only have a tree, without a table. Therefore, let's now add two extra columns, via the new "org.netbeans.swing.outline.RowModel" class, which will enable the creation of a tree table instead of a tree:

private class FileRowModel implements RowModel {

@Override
public Class getColumnClass(int column) {
switch (column) {
case 0:
return Date.class;
case 1:
return Long.class;
default:
assert false;
}
return null;
}

@Override
public int getColumnCount() {
return 2;
}

@Override
public String getColumnName(int column) {
return column == 0 ? "Date" : "Size";
}

@Override
public Object getValueFor(Object node, int column) {
File f = (File) node;
switch (column) {
case 0:
return new Date(f.lastModified());
case 1:
return new Long(f.length());
default:
assert false;
}
return null;
}

@Override
public boolean isCellEditable(Object node, int column) {
return false;
}

@Override
public void setValueFor(Object node, int column, Object value) {
//do nothing for now
}

}

Now, after dragging-and-dropping an Outline object onto your JFrame (which is possible after adding the beans from the JAR to the NetBeans IDE Palette Manager) which, in turn, automatically creates a JScrollPane as well, this is how you could code the JFrame's constructor:

public NewJFrame() {

//Initialize the ui generated by the Matisse GUI Builder, which,
//for example, adds the JScrollPane to the JFrame ContentPane:
initComponents();

//Here I am assuming we are not on Windows,
//otherwise use Utilities.isWindows() ? 1 : 0
//from the NetBeans Utilities API:
TreeModel treeMdl = new FileTreeModel(File.listRoots()[0]);

//Create the Outline's model, consisting of the TreeModel and the RowModel,
//together with two optional values: a boolean for something or other,
//and the display name for the first column:
OutlineModel mdl = DefaultOutlineModel.createOutlineModel(
treeMdl, new FileRowModel(), true, "File System");

//Initialize the Outline object:
outline1 = new Outline();

//By default, the root is shown, while here that isn't necessary:
outline1.setRootVisible(false);

//Assign the model to the Outline object:
outline1.setModel(mdl);

//Add the Outline object to the JScrollPane:
jScrollPane1.setViewportView(outline1);

}

Alternatively, without the NetBeans Matisse GUI Builder and NetBeans Palette Manager, i.e., simply using a standard Java class, you could do something like this:

private Outline outline;
public NewJFrame() {

setDefaultCloseOperation(EXIT_ON_CLOSE);

getContentPane().setLayout(new BorderLayout());

TreeModel treeMdl = new FileTreeModel(File.listRoots()[0]);

OutlineModel mdl = DefaultOutlineModel.createOutlineModel(
treeMdl, new FileRowModel(), true);

outline = new Outline();

outline.setRootVisible(false);

outline.setModel(mdl);

getContentPane().add(new JScrollPane(outline),BorderLayout.CENTER);

setBounds(20, 20, 700, 400);

}

At this point, you can run the JFrame, with this result:

So, we see a lot of superfluous info that doesn't look very nice. Let's implement "org.netbeans.swing.outline.RenderDataProvider", as follows:

private class RenderData implements RenderDataProvider {

@Override
public java.awt.Color getBackground(Object o) {
return null;
}

@Override
public String getDisplayName(Object o) {
return ((File) o).getName();
}

@Override
public java.awt.Color getForeground(Object o) {
File f = (File) o;
if (!f.isDirectory() && !f.canWrite()) {
return UIManager.getColor("controlShadow");
}
return null;
}

@Override
public javax.swing.Icon getIcon(Object o) {
return null;

}

@Override
public String getTooltipText(Object o) {
File f = (File) o;
return f.getAbsolutePath();
}

@Override
public boolean isHtmlDisplayName(Object o) {
return false;
}

}

Now, back in the constructor, add the renderer to the outline:

outline1.setRenderDataProvider(new RenderData());

Run the JFrame again and the result should be the same as in the first screenshot above. Look again at the rendering code and note that, for example, you have tooltips:

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Steve Sdas replied on Thu, 2012/11/15 - 7:09am

I had hoped that the darkness of the sky would mean a reprieve from the “sports camp” my father believed should be summerpisos para gimnasio

Steve Sdas replied on Thu, 2012/11/15 - 4:04pm

I have to say that I am really very impressed with your amazing talent. This was really very cool.piso para bebes

Steve Sdas replied on Sun, 2012/11/18 - 5:07am

Context-dependent colours! Tool tips! Although not shown, it can also do custom icons and show special display names!Tapetes para areas humedas

Uilly Tocky replied on Tue, 2012/12/04 - 12:43pm

Your affect is totally appreciative and newsy. Bathroom renovating

Uilly Tocky replied on Sun, 2012/12/09 - 6:15am

Recent times when net has so often gossip mongering and clog; your proportion really refreshes me. hair salon Chelsea NYC

Uilly Tocky replied on Sun, 2012/12/30 - 2:55pm

Thanks for shearing Pretty great post !! That's a great thinking for the tips on travel for tourist consciousness person.ppi claims credit cards

Bryan Low replied on Tue, 2013/01/08 - 10:18am

Stratum Condo

For vehicle entrepreneurs, it takes less than 30 minutes to force to the business hub and stunning Orchard Road buying area, via Kallang-Paya Lebar Expressway (KPE) and Southern Coast Expressway (East Coast Expressway).

Bryan Low replied on Fri, 2013/01/18 - 1:16am

Several buses are available near Potong Pasir along with shopping centers and restaurants. Sant Ritz is also near to Volkswagen Golf Centre and Kallang Side Riverpark. Entertainment for your loved ones and friends is therefore at your fingertips with the full condo facilities as well as the amenities near Potong Pasir.

Sant Ritz

Bryan Low replied on Fri, 2013/02/01 - 9:59pm

Twin Fountains EC facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Woodlands.

Twin Fountains

Uilly Tocky replied on Sat, 2013/02/02 - 6:47am

Useful information ..I am very happy to read this article..thanks for giving us this useful information.nursery school program

Uilly Tocky replied on Wed, 2013/02/06 - 9:05am

Your blogs are totally worth giving time and energy. reverse phone lookup

Bryan Low replied on Sun, 2013/02/24 - 4:54am

Bartley Ridge will be accessible via Bartley MRT station on the Circle Line. Commuting to Toa Payoh and Paya Lebar area as well as the city area is therefore very convenient. It is also near to many eateries along the Upper Serangoon area as well as NEX shopping mall.

Bartley New Launch 

Roger Sall replied on Tue, 2013/04/09 - 1:33pm

Telexfree I found an example from some time ago, by Tim, and have been playing with it to get used to this new development. The result is as follows: Not trust

Junio Mousull replied on Mon, 2013/04/15 - 10:32pm in response to: Steven Baker

Baker Esteven great subject. Few write as well as you roupas para revenda

Junio Mousull replied on Mon, 2013/04/15 - 10:35pm in response to: Steve Sdas

Bloggou take the hand table and writer good Esteve

Danish12 Ali replied on Fri, 2013/07/26 - 3:36pm

 Before you search the information in the internet, you need to think what your need for the holiday is. Do you want budget holiday? Or do you want holiday with high standard of accommodation? Per the information provided, you can then select and use the camping holiday smartly.  no flame e cigarette

Desmond Tan replied on Wed, 2013/09/11 - 10:25pm

Entertainment for your loved ones and friends is therefore at your fingertips with the full condo facilities as well as the amenities near The Inflora Condo. Inflora home

Interlace Pet G... replied on Tue, 2014/03/25 - 10:07pm

A wonderful and unique lifestyle awaits you. Please see The Interlace project details and floor plans for more information. The Interlace condo video

Interlace Pet G... replied on Tue, 2014/03/25 - 10:08pm

The Interlace has full and unique facilities, which includes a guard house, clubhouse, Function Room & Indoor Gym Tennis Court, 50m Freeform Pool Pool Deck, Wading Pool, Splash Pool & Family Pool Jacuzzi & Hydro Spa, BBQ Area Dining and Play Fountain, Fitness Alcove & Children’s Playground and Garden Trail. highline private preview kim tian

Muhammad Danial replied on Fri, 2014/03/28 - 3:58am

 you just provide a standard Swing TreeModel of whatever sort you like, and an additional RowModel that can be queried for the other columns contents, editability and so forth.

Richard Warke profile

Muhammad Danial replied on Wed, 2014/04/02 - 5:20am

 OutlineModel" which, in turn, is built from a plain old javax.swing.tree.TreeModel, together with the new "org.netbeans.swing.outline.RowModel . 

Business Financial Statements

Samad Khatri replied on Wed, 2014/04/02 - 7:09am

RowModel that can be queried for the other columns contents, editability and so forth.forbrukslån på dagen

Muhammad Danial replied on Thu, 2014/04/03 - 11:17am

 OutlineModel" which, in turn, is built from a plain old javax.swing.tree.TreeModel, together with the new "org.netbeans.swing.outline.RowModel". Optionally, to change the default rendering, you can use the new

Read this page

Samad Khatri replied on Thu, 2014/04/03 - 11:35pm

 RowModel that can be queried for the other columns contents, editability and so forth https://www.rebelmouse.com/mikefiorecapturehisheartreview/

Muhammad Danial replied on Sat, 2014/04/05 - 2:48am

 Tim, and have been playing with it to get used to this new development. The result is as follows: 

Check this link

Muhammad Danial replied on Sat, 2014/04/05 - 6:08am

 For the rest, I wasn't required to do anything with NetBeans, necessarily. I could have attached the JAR to a project in Eclipse or anywhere else. Then I created a JFrame. 

boys camouflage bedroom ideas

Muhammad Danial replied on Mon, 2014/04/07 - 6:21am

 I found an example from some time ago, by Tim, and have been playing with it to get used to this new development. The result is as follows:

energía solar

Commonwealth Towers replied on Mon, 2014/04/07 - 9:01pm

The biggest reason why Singapore has gone from position 17 in 2005 to the most expensive in 2012 is due to the exchange rate effect,” said Tan Kee Giap, ACI’s Co-director for Public Policy. detached

Samad Khatri replied on Tue, 2014/04/08 - 12:25am in response to: Gustav Weber

 To get started, I simply download the latest NetBeans IDE development build from netbeans.org and then attached  lisa olson pregnancy miracle

Samad Khatri replied on Tue, 2014/04/08 - 5:54am

 To get started, I simply download the latest NetBeans IDE development build from netbeans.org and then attached  team ifit 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.