Jun 062012

I recently started using the excellent ICEpdf library to display PDFs in a Java application. And as invariably happens with most libraries, I wanted to do something that is just not documented anywhere yet. 🙂 Or at least anywhere that I can find…

But lets first look at the available documentation:

I am using the Viewer, creating the panel as per the first link above. But I want to configure which buttons are visible on the toolbar at run time, according to user settings persisted in a database. This rules out the option of using the config file route. And hence I needed to find a way to change the configuration programmatically.

So I debugged into the source code, and I found out that there is indeed an easy way. 🙂

SwingController controller = new SwingController();
PropertiesManager propertiesManager = new PropertiesManager(
        System.getProperties(), new Properties(), 
        PropertiesManager.PROPERTY_SHOW_UTILITY_SAVE, false);
SwingViewBuilder factory = 
        new SwingViewBuilder(controller, propertiesManager);
JPanel viewerComponentPanel = factory.buildViewerPanel();

Jun 062012

Every so often I manage to do something really silly in the GUI builder, which causes unexpected behaviour (and exceptions) when the form is actually executed. And today I realised again that it is indeed sometimes important which step you choose to associate a piece of code with. For example, accidentally trying to set a property on the component in the pre-creation step would fail since the component just doesn’t exist yet at that point.

So the tip I want to share with you today is this. If you encounter a component that does something unexpected (or doesn’t as the case may be), then start off by looking at it using the Code Customizer. It can be accessed by right-clicking on the component and choosing Customize Code…

Code Customizer showing silly error

The Code Customizer is a handy view showing you the source code related only to the current component, in the order in which it will be executed. And if you read through the code in sequence there, many errors will become very obvious indeed. 🙂

Of course, setting the text as in the above example should rather be done by setting the text property. Then the GUI builder will take care of setting it properly. This is intended just as an example of the type of issue that you could pick up in this way.

For more information about the NetBeans GUI Builder, have a look at the Introduction to GUI Building tutorial.