Download NetBeans!

20070607 Thursday June 07, 2007

Listening to Text Fields and Checking Image Dimensions

Issue 11 in yesterday's blog entry presents an interesting problem: "No check is done to make sure that the selected icons are of the correct dimensions. So, if the small icon that you select is extremely large, there's nothing protecting you from making that mistake."

Imagine that the user chooses a very large icon and you perform no check to prevent that. The Preview panel at the bottom of the dialog box would be covered by the new icon, which would be very ugly, and the icon attached to the node in the palette would be massive and not useful at all. Here's how the Preview panel would look if I chose a really large image:

However, what should happen instead is that a red error warning should appear, and the massive icon should not be allowed to appear in the Preview panel at all, as shown here, where the default icons are still shown, even though a massive icon has been selected:

How to code this scenario? Firstly, none of the code involved makes use of the NetBeans APIs. It is all just plain vanilla JDK code. Attach a document listener to the text fields, evaluate the dimensions of the selected icons as their locations are being inserted in the text fields, and then take appropriate action. I discovered how to do this by looking in the NetBeans sources, because the New File Type wizard makes use of the same functionality. There, if you choose an icon with the wrong dimensions, you get this warning message:

Wouldn't it be cool if the utility methods used for creating the above warning were to be made available to the NetBeans APIs? (That would result in this functionality being used in other API wizards too, because currently there are several wizards where no such check is done, although the need for it there is the same as in the New File Type wizard.)

But the real culprit is the JFileChooser. In my opinion, the JFileChooser should display some details about whatever file is currently selected within it. For example, when an image is selected, the JFileChooser should display its dimensions, so that at that stage I can decide to choose a different image, instead of later (too late) when I have completed my work in the JFileChooser. However, since Swing is now open sourced, maybe someone should enhance the JFileChooser. And you can even use the IDE to do so, since the Swing sources are available as a NetBeans project.

Jun 07 2007, 03:44:51 AM PDT Permalink