Why NetBeans is awesome. Part 1: Swing form editor and custom components.

I’m great fan of NetBeans IDE and think it really doesn’t get enough noise. So I’m starting my Why NetBeans is awesome series of posts that show some NetBeans features you might not know about, but are really cool or makes life really easier.

First in the series is about great Java Swing form editor and how you can use it with custom components you have created in your application. What NetBeans allows you to do is to just add your components to palette and drag’n’drop it anywhere you need it. What’s coolest there is that it will look exactly like you designed it and will allow you to operate on properties (via getter/setter you have in your component) using Properties table you’re used to while working with ootb Swing components. Here’s how…

Create component

You can just compose it using NetBeans form editor. Only no-args public constructor is required.

I’ve made it little bit fancy here so you’ll see that it’s rendered when dropped exactly as you will see it when running your application.
Next I’ve added getter/setter for properties I want to have editable in Properties table:

    public Color getCheckBoxColor() {
        return confirmedCheckBox.getBackground();
    }
    
    public void setCheckBoxColor(Color color) {
        confirmedCheckBox.setBackground(color);
    }
    
    public void setButtonText(String text) {
        button.setText(text);
    }
    
    public String getButtonText() {
        return button.getText();
    }

Remember to compile form before using it in palette.

Add component to Palette

Just use Add to Palette action:

…and here is what it looks like:
You might need to refresh Palette after adding new component to it (right click menu on Palette)

Use it just like ootb Swing component

Now you can just drop it somewhere in other components:

Have you noticed that it looks just like you’ve designed it? This tool really rocks!
Lets position it little bit better and check if our attributes are in Properties table:

They’re here! And you know what? They contain values that getters return!
Now the best. You can edit them in Properties table and it will appear immediately in current form!
What I’ve done is just edited values in table and everything was updated in the form! No recompile needed, no refresh or reload. It’s just updated!

Color property is editable with color picker. It’s also marked as edited!

I really like NetBeans form editor. It makes Swing application design so easy. You should try it if you haven’t yet.
I’m using it every day for production applications and got no problems with code it generates. You can even add your custom layout managers and set them on containers. You know what? Components will be layed out in form editor exactly like it will be layed out after starting your application.

Advertisements

One Response to Why NetBeans is awesome. Part 1: Swing form editor and custom components.

  1. Anonymous says:

    I just was just thinking netbeans is awesome! and while thinking i googled it. and got here.
    I’ve not read much of your article cause i am going to sleep now, will read later, but the thing that surprised me is that there is some bigger fan of netbeans than me 😛

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: