Creating Visual Java Applications

My excellent colleague Sun evangelist Chuk Munn Lee wrote me an e-mail in response to my description yesterday of the JConsole plugin: "I did not know that the visual library can be used outside of NetBeans. Do you need a special build of the library? Do I need to build it myself or is there a standalone version that I can download?"

This is a good question and the answer is illustrative of how interwoven NetBeans IDE is with the NetBeans Platform. Look in your NetBeans installation folder and you'll find a folder called "platform7", with this content:

In other words, literally, the content of the folder above is the NetBeans Platform. Many of the JARs that make up the NetBeans Platform can be used outside a NetBeans Platform application. Last October I wrote about this, in a blog entry entitled NetBeans APIs Outside of the NetBeans Platform. Follow the steps in that tutorial and you'll have a standard Java application that makes use of some of the typical JARs from the NetBeans Platform.

In other words, several typical scenarios developed on the NetBeans Platform can also be developed outside of it. Another similar scenario is illustrated in my blog yesterday, where the Visual Library API is used to inject some graph functionality into the JConsole. And so, in response to Chuk's questions "Do you need a special build of the library? Do I need to build it myself or is there a standalone version that I can download?", the answer is: "No, you don't." Download NetBeans IDE, take the two JARs that are highlighted in the screenshot above, put them on your app's classpath, and you're ready to create visual Java applications:

Let's get started for real now. To create visual applications, you need some kind of Swing container within which you need a JScrollPane. Then you create your visual "scene" within that JScrollPane. There are several interesting classes that you can extend to make your visual application. Have a look at the Javadoc or take a stroll through the tutorial, though not all of the tutorial is intended for use outside of the NetBeans Platform.

Now that you have a Swing container with a JScrollPane, create a new Java class that extends GraphScene. Fill it out as follows:

package demo;

import java.awt.Image;
import java.awt.Point;
import java.util.Random;
import javax.swing.ImageIcon;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.api.visual.widget.general.IconNodeWidget;
import org.openide.util.Utilities;

public class DemoGraphScene extends GraphScene {

    private LayerWidget mainLayer;
    private Image image;
    private Random r = new Random();

    public DemoGraphScene() {
        mainLayer = new LayerWidget(this);
        addChild(mainLayer);
        image = Utilities.icon2Image(new ImageIcon(getClass().getResource("/demo/chuk.png")));
        addNode("Chuk");
    }

    @Override
    protected Widget attachNodeWidget(Object arg0) {
        IconNodeWidget widget = new IconNodeWidget(this);
        widget.setImage(image);
        widget.setPreferredLocation(new Point(10, 20));
        mainLayer.addChild(widget);
        return widget;
    }

    @Override
    protected Widget attachEdgeWidget(Object arg0) {
        return null;
    }

    @Override
    protected void attachEdgeSourceAnchor(Object arg0, Object arg1, Object arg2) {
    }

    @Override
    protected void attachEdgeTargetAnchor(Object arg0, Object arg1, Object arg2) {
    }

}

Most of the above code is self-explanatory, I believe. You have a LayerWidget which you add to the scene. Then you add an IconWidget, which you add to the layer. One interesting thing to note is how the icon is converted to an image, via a utility method from the NetBeans Utilities API, which you have already put on your classpath, together with the Visual Library API. In other words, there's a bunch of stuff that NetBeans Platform developers use that can also be used in your own smaller Java applications, as is the case here.

To wrap up, we need to instantiate the GraphScene class from our JFrame and then set the JScrollPane so that its port view will contain the "scene". Here's how we do that, nice and easy via the JFrame constructor:

private DemoGraphScene scene = new DemoGraphScene();

public DemoVisualFrame() {
    initComponents();
    jScrollPane1.setViewportView(scene.createView());
}

Next, I added a pic of Chuk to my app's source structure and then ran the application. And here's the result:

Next, let's turn Chuk into a movable object. The user should be able to drag and drop him with their mouse. Hmmm. That will mean a lot of coding, right? Wrong. Here's all I needed to add, i.e., just the line in bold:

protected Widget attachNodeWidget(Object arg0) {
    IconNodeWidget widget = new IconNodeWidget(this);
    widget.setImage(image);
    widget.setPreferredLocation(new Point(10, 20));
    widget.getActions().addAction(ActionFactory.createMoveAction());
    mainLayer.addChild(widget);
    return widget;
}

And now, when I run my app, I can use my mouse to drag Chuk to a different location:

A MoveAction can only mean one thing, i.e., it can only mean that you want to move the widget. However, other actions could be implemented in a variety of ways, hence there is no default. You need to provide the content of the action yourself, as in the case of the LabelTextFieldEditor. Here, we begin by defining a class-level LabelTextFieldEditor:

private WidgetAction editorAction = ActionFactory.createInplaceEditorAction(new LabelTextFieldEditor());

Next, we define out LabelTextFieldEditor, extending the TextFieldInplaceEditor class:

private class LabelTextFieldEditor implements TextFieldInplaceEditor {
    public boolean isEnabled(Widget widget) {
        return true;
    }

    public String getText(Widget widget) {
        return ((LabelWidget) widget).getLabel();
    }

    public void setText(Widget widget, String text) {
        ((LabelWidget) widget).setLabel(text);
    }
}

Finally, we add a label to our widget. We also assign the editor action defined above to our widget. Here we go:

@Override
protected Widget attachNodeWidget(Object arg0) {
    IconNodeWidget widget = new IconNodeWidget(this);
    widget.setImage(image);
    widget.setPreferredLocation(new Point(10, 20));
    widget.setLabel("Chuk");
    widget.getLabelWidget().getActions().addAction(editorAction);
    widget.getActions().addAction(ActionFactory.createMoveAction());
    mainLayer.addChild(widget);
    return widget;
}

That's it. Let's run our app again. You now have a label which you can click and then it is editable:

When you press Enter, the label is changed.

But what's the point of having just Chuk in our application? Let's add another evangelist, Gregg Sporar:

private Image CHUK = Utilities.icon2Image(new ImageIcon(getClass().getResource("/demo/chuk.png")));
private Image GREGG = Utilities.icon2Image(new ImageIcon(getClass().getResource("/demo/gregg.png")));

Next, we'll create a hash table, so that we can manipulate them more effectively:

private final Hashtable mapping;
{
    mapping = new Hashtable();
    mapping.put("Chuk", CHUK);
    mapping.put("Gregg", GREGG);
}

Then, we'll add them both, rather than just one:

public DemoGraphScene() {
    mainLayer = new LayerWidget(this);
    addChild(mainLayer);
    addNode("Chuk");
    addNode("Gregg");
    getActions().addAction(editorAction);
}

We'll need to rewrite our earlier method just a bit, so that it is more generic, because now it will have to handle both Chuk and Gregg:

@Override
protected Widget attachNodeWidget(Object node) {
    Image image = (Image) mapping.get(node);
    if (image != null) {
        return createNewWidget(node, image);
    }
    throw new IllegalArgumentException(node.toString());
}

And we move all the code from the previous implementation of the above method to a new one:

protected Widget createNewWidget(Object label, Image image) {
    IconNodeWidget widget = new IconNodeWidget(this);
    widget.setImage(image);
    widget.setPreferredLocation(new Point(10, 20));
    widget.setLabel(label.toString());
    widget.getLabelWidget().getActions().addAction(editorAction);
    widget.getActions().addAction(ActionFactory.createMoveAction());
    mainLayer.addChild(widget);
    return widget;
}

Run the app again and now you'll have two evangelists in your "scene", instead of just one, with the same properties because they're both the same widget, as you can see from the code above:

You might now think about connecting them together, which we can look at in a future blog entry. In general, there's a lot more that can be done, of course, and the documentation on all of this goes into it all. Here's the homepage of the library. I also highly recommend Fabrizio Giudici's Creative use of the NetBeans Visual Library: the Light Table.

In summary, you don't even need to like NetBeans IDE to benefit from its graph library. Just download the IDE, get the two JARs specified above, and then add them to your classpath. That's all. And then code in whatever IDE has your preference.

Comments:

Hey, how come Chuk's picture is larger? :-)

Posted by Gregg Sporar on January 29, 2008 at 05:36 AM PST #

I just copied your pic from your blog... And you know what they say about size. :-)

Posted by Geertjan on January 29, 2008 at 03:27 PM PST #

Visual Library (and graph.netbeans.org, with the old name it had before) has always worked in a standalone fashion, needing only openide-util.

I know I used it in the University 2 years ago for some projects and it sure made them look cool (impressing teachers always helps ;) ).

Posted by Emilian Bold on January 29, 2008 at 03:38 PM PST #

Hi Geertjan

Since i read your post in the end of January i have been looking for an excuse for using this module.

Now finally i have come up with one, but in /NetBeans 6.1/platform8/modules/ext i can't find the Jar.
Has the module been moved and i should just check out my older instalations (platform7) or..?

Now that i have the need for the module, it would be sad if it were not any longer available..

Posted by Anders on September 14, 2008 at 07:46 PM PDT #

Which JAR can't you find? Both of them are there.

Posted by Geertjan on September 14, 2008 at 07:59 PM PDT #

Shoot - i need more coofee!
Thx
/Anders

Posted by Anders on September 14, 2008 at 09:04 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today