NetBeans Gotcha: GUI custom component problem when package and class named the same

Let’s say you have a project with package Example, and in that package you have:

Example.java       // troublemaker!
ExampleUI.java     // "form" - extends JFrame
LabelExample.java  // extends JLabel

One of the cool things about the Matisse GUI Editor in NetBeans is that you can add LabelExample.java as a custom component to the widget palette and then drop an instance of it on your ExampleUI.java form in design mode. NetBeans will generate the source code for you.

However, there is a little hitch. When NetBeans creates the variable declaration, it will use the fully qualified path of your class, in this case Example.LabelExample, and then the IDE (which is chanelling the compiler), will complain with:

NetBeans GUI Gotcha

That is, here is your generated code block:

// Variables declaration - do not modify
private Example.LabelExample labelExample1;
// End of variables declaration

And this is the error:

cannot find symbol
symbol  : class LabelExample
location: class Example.Example

I guess it’s confused about which “Example” you mean, the package or the class. In the screenshot above, you can see that private LabelExample labelExample2; doesn’t have the same problem. Normally I wouldn’t use the full path in this situation, but in this case the IDE is choosing it for you, and you can’t modify that code block, so you’re stuck. (Well, I seem to remember seeing that there is some way to modify it, but it’s not recommended.)

It seems obvious now, but I really stumbled and bumbled around with this one on my way to enlightenment. I’m hoping to save at least one person the same frustration. Maybe I should mention that I was hung up on a JPanel component, in case that’s what you’re searching for. :-)

It looks like this is only a problem if you have just that one layer of package. Package Sample.Example with Example.java works fine. In general I construct my packages with lower case directory names, but not always on the leaf package dir, and not always on a simple project where I just have a single directory package. (Maybe I should change my ways, although now I know better in this one unusual situation.)

If you enjoyed this article, please subscribe for free!
Via the atom or rss feed, or enter your email address to get updates when new entries are posted:
(Your email will not be shared nor used for anything other than sending new posts. See the policies page for more about subscriptions and privacy.)

You can skip to the end and leave a response. Pinging is currently not allowed.

No comments yet.

You can follow any responses to this entry through the
comments feed.

Say Your Say

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

By submitting your comment here, you agree to license it under the same Creative Commons Attribution-ShareAlike 3.0 License as the movingtofreedom.org web site. Please see policies for more information about comments and privacy.