November 25, 2009

NetBeans Platform: Output windows in simple words

I/O APIs is among the most used modules from NetBeans Platform. This is one of the most common way to show information about your activity. The name can be misleading, but is much simpler that it seems. This module manages the output windows which you already have or you can create, where text information can be put.
There are a a lot of scenarios where you need output windows. You need them to show logs for bootstrapping tasks, to show log information, to receive feedback from a compiler or builder or other type of tool.

The story about output windows in NetBeans can be simple or complicated. The hard way is to implement yourself everything and control everything. That could be feasible if you plan to implement a rich output windows with a lot of controls on it. But for simple scenarios, what you have from the platform is simple enough.

Setup

From your NetBeans Platform module, all you have to do is to add "I/O APIs" as a module dependency. That's all.

"Hello world!" from output window

IOProvider is a factory for output window components. These output windows are represented by InputOutput class and hosted into a container, represented by IOContainer. It's easy and enough to proceed. First, we ask for the default provider to get an InputOutput with a specific title. We can create a new one or get an existing one. After that we activate the output and write to it. Here's the code.

InputOutput io = IOProvider.getDefault().getIO("TestIO", true);
io.select();
io.getOut().print("Hello world!");

Put some colors and behavior on lines

We can easily color the line from output. Also we can put some action on the line. If we create a line with an listener (our listener), the line from output would behave as a hyperlink in a browser. When the listened line will be selected, clicked or deleted, we can do something through listener. Let's see some more code.

try {
    InputOutput io = IOProvider.getDefault().getIO("My title", false);
    IOColorLines.println(io, "This is a yellow line", Color.yellow);
    IOColorLines.println(io, "This is e red important line",
                    null, true, Color.red);
    IOColorLines.println(io, "one dynamic line with event",
        new LineListener(), true, Color.green);
} catch (IOException ex) {
    Logger.getLogger(SameIOAction.class.getName()).log(
        Level.SEVERE, null, ex);
}

[...]
class Listener implements OutputListener {
    public void outputLineSelected(OutputEvent ev) {
        JOptionPane.showMessageDialog(null, "line with content " +
                ev.getLine() + " was selected");
    }
    public void outputLineAction(OutputEvent ev) {
        JOptionPane.showMessageDialog(null, "line with content " +
                ev.getLine() + " was actioned");
    }
    public void outputLineCleared(OutputEvent ev) {
        JOptionPane.showMessageDialog(null, "line with content " +
                ev.getLine() + " was cleared");
    }
}

Put some decorations on output windows

Using another class from the package, IOTab, you can easily add a tooltip text and a small icon on an output window. As a sample:

InputOutput io = IOProvider.getDefault().getIO("My title", false);
IOTab.setToolTipText(io, "My tooltip text for Hello world!");
[..]


And you can do more..

There is also a simple way to add some Swing actions on output window. The output window has a small toolbar on the left side of it. If you pass an array of Swing Actions on the moment of creation, these actions will be available for use on your output window. Just to name a very often scenario: you have a long process to run, you want to give full text feedback, but you want to be able to stop the process when you think is appropriate. Just create a Swing Action to fire the canceling job and add it to the InputOutput.

Action[] actionList = new Action[5];
[..] // add actions here
InputOutput io = IOProvider.getDefault().getIO("My title", actionList);

Thought there are some limitations: you can't put here more than 5 actions and each action should have the property Action.SMALL_ICON defined.

Have fun with InputOutput windows!

2 comments:

  1. Thank you Aurelian,
    I was searching for Netbeans Output window howto luckly i found your blog It is very useful for me.

    ReplyDelete
  2. AnonymousJuly 29, 2011

    Hi
    Is there a way to erdirect standard output (i.e. the one in the netbeans ide) to the output window of my netbeans platform application? So whenever I write System.output.println("something") it appears in the output window?
    Thanx

    ReplyDelete