The Source for Java Technology Collaboration
User: Password:



Joshua Marinacci

Joshua Marinacci's Blog

Easily Use Custom Swing Components with Matisse

Posted by joshy on February 07, 2006 at 03:08 PM | Comments (9)

I know it's been quite a while since I've written anything. I've been busy with quite a few project and I'll have some cool stuff to share soon. In the meantime I thought I'd show you an interesting tidbit I discovered in Matisse and Netbeans.

I've recently begun using Matisse for all of my GUI building tasks and overall I'm pretty pleased. I can quickly whip up dialog boxes and basic layouts very easily. However, I often use custom components and it's not easy to integrate them with Matisse. Sure, I could package my components up as full Java Beans and install them in the palette, but that's a whole lot of work for a single instance or two. I don't really want to have Matisse create the component for me, I just want the ability to use it's layout.

In the past I have created a panel with a zero width border and then put my custom components inside that. This will work most of the time but it still doesn't feel quite right. I'd rather have Matisse lay out my real component. I can't quite do that but I have found a nice little workaround that's pretty close.

The property palette has three tabs: Properties, Events, and Code. On the code tab there is a property (probably the last one) called Custom Creation Code. This property will let you use a custom constructor and initialization code. If you leave it blank then Matisse will generate something like "new JButton()" but you can override it to use your own code, as long as the result is some compatible class.

This may all sound a bit confusing so lets go with an example. I have a custom JButton subclass that I use for selecting colors. It's just like the normal JButton except it overrides the paintComponent() method to draw a box with the current background color. When you click on the button it will open a JColorChooser dialog to select a new color. Simple enough and not something I'd want to add to the designer palette. Instead I'll use the custom creation code.

I start by creating a normal JButton in the designer like this:

a custom JButton in design mode

Then I go to the Custom Creation Code property in the code tab of the Properties palette and type in: new ColorSelectionButton(); Note that I didn't type in the jButton1 = part.

the Custom Creation Code property

If I switch to code view and look at the generated code I can see this.


    private void initComponents() {
        jButton1 = new ColorSelectionButton();

        jButton1.setText("jButton1");

Matisse Generated Code

Then when I run my program it will look like this:

Running Program

This system works great and I think I'm going to start using it from now on. It does have a couple of downsides though:

  1. You can only use the parent class of your component. Your custom component will be cast down to a standard one so if you use a custom JButton then you must use a JButton in the designer rather than, say, a JLabel. As a consequence..
  2. You can't use direct subclasses of JComponent because there is no JComponent in the standard palette. Most of the time this is fine (I usually subclass JButton or JPanel), but it might be a problem depending on your code.
  3. Your preview in design mode won't match the running application. Matisse does a very good job of making design and runtime views match up, but using this hack defeats Matisse's efforts. In my example I see my custom button at runtime but still get the standard button in the design view.

So it's not perfect, but for most cases I have found it to be quite acceptable and plan to use this technique more in the future.


Update

Approximately 4.8 milliseconds after I posted this gfx informed me that Netbeans actually has a way to add components to the palette without creating a full Java bean. :) You can use the Palette Manager to add any public component class to the palette. *sigh*. Anyway, using the palette only works if you have a public class with no-arg constructor, so my technique is still useful if you want to use a special constructor or an anonymous inline subclass.

Enjoy!

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Good 1 Josh, y don't u start to write netbeans matisee tutorials.You write much better then all current netbeans guys. ( :-) ).

    Posted by: hithacker on February 08, 2006 at 04:17 AM

  • ah - was just about to write that you could add your class to the alette, but then noticed you'd added that in your update. Maybe the "update" should go at the top of the page, since the rest of the article is a bit misleading for people unaccustomed to Netbeans ?

    One problem I do have with the netbeans/matisse palette, though, is that matisse doesn't easily allow you to add custom swing classes that exist in your current project - I would have thought that it would be simple to search your project for JComponent subclasses , for instance, and make them automatically available in Matisse. My guess is that the reason it isn't implemented is that there would be issues dealing with keeping track if you changed the API of your component - it might break the code written by Matisse - but it shouldn't be *too* hard to fall back relatively gracefully in these circumstances.

    Posted by: jportway on February 08, 2006 at 09:26 AM

  • Hi Joshua , Ur articles(especially ur hacks) are great.....I recently read ur book
    SWING HACKS some of the hacks were tooo good. Any Plans for the SECOND Edition of Swing Hacks. I just want to read many more of ur wonderful hacks....

    Posted by: psychostud on February 08, 2006 at 10:25 AM

  • Hi,
    is there any more detailed tutorial how Matisse works, i do not see the big benefit against JBuilders gui editor ?
    I'd like to use it may in one of my new projects only for the GUI design step.

    regards,
    jens

    Posted by: mac_systems on February 08, 2006 at 12:11 PM

  • It'd be nice if one could change the default JButton construction code without having to go to every placed JButton. The use case for this is I want some custom initialization for every JButton. I could create a JButton factory method and then change the default JButton construction code to use this factory.

    I've just started playing with Matisse and I see many things that could be improved to make it more useful for production-quality code.

    Posted by: neilweber on February 17, 2006 at 09:28 AM

  • Thanx for that. But I think NetBeans should have a clear way to add custom classes to Swing palette

    Posted by: vaddie on October 24, 2006 at 09:58 AM

  • You can already, actually. If you right click on a class in the project inspector you can add it to the palette. In NB 6 I think you can actually just drag a class to the palette or directly into your panel to add it.

    Posted by: joshy on October 24, 2006 at 10:22 AM

  • Thanks!! I'm new to Netbeans and have been scouring the web for an answer to this. I was starting to think I'd have to abandon Matisse and go back to hand coding the layout - yuck!

    Posted by: porjo on November 25, 2006 at 01:58 AM

  • Nice tip. Your approach, and the custom palette approach, have different advantages and drawbacks, so I think it's good that both are documented.

    I will mention a third approach, which seems the least friendly of the bunch. But it may help someone somewhere, who knows? I have only tried this in NB6.0.

    If you right click in the Inspector, on the component you would like to add your component to, and choose "Add From Palette". From there, go to the "Beans" option. Then click "Choose Bean..."

    NetBeans will prompt you for a class name. You can enter the (fully qualified?) class name of your custom component there, and NetBeans adds it without complaining too much!

    It's a little rough around the edges, but since it's so free form, it may work in certain situations where the other options don't.

    Posted by: mikeyc on January 08, 2008 at 11:31 AM



Only logged in users may post comments. Login Here.


Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds