File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Class creation with generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class creation with generics" Watch "Class creation with generics" New topic

Class creation with generics

David Nemeskey
Ranch Hand

Joined: Nov 08, 2006
Posts: 52

I am writing a helper class for GridBagLayout-based forms. It's almost finished, I just can't decide how to let it know the Container type I would like to use.

Basically the class should be used like:
1. instantiation: it gets a Container
2a. add rows by the addRow(Component c1, Component c2) method
2b. add empty space by the addSpace(int pixels) method
3. get the form by calling getForm(). This finalizes the Container, and no changes are accepted after this.

The question is about the instantiation. I have two ideas:


The first makes it possible the hide the container all along, so that the construction is safe -- nobody can meddle with it. However, it looks ugly, and I must throw an InstantiationException, which I cannot catch at any sensible place in the program. Why should any level in the application have to care about this? It's the same as, say, a new JPanel() failing...

The second alterative eliminates this exception, but then the reference to the container being built is public, and someone can ruin it.

What do you think, which is the better solution?

P.S. I think it is not a GUI question, more of a question about generics, that is why I put it here.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
Not sure about all your queries.
I am not convinced that InstantiationException is the right Exception to use, you might have to create your own. Keep it checked. And don't catch it. There is no point in your helper class catching it; throw it back to the place you were called from and let that Class deal with it.
As for helper classes, are you familiar with Cay Horstmann's GBC class? That makes using GridBag a lot easier.

I hope that is of some help to you. CR
David Nemeskey
Ranch Hand

Joined: Nov 08, 2006
Posts: 52

maybe I was unclear on the exception part. Class.newInstance() throws InstantiationException, that is why the first constructor type must throw it as well. My main concern is, that

looks ugly; that's exactly one more JPanel than there should be. However, the other approach brings up (real) problems, that is why I am hesitating which of them I should use.

I have had a look at the class you mentioned. Thanks, but there is really nothing more than just a few convenience methods to it, so that you can write

instead of

Anyway, please consider the question in general, with the GUI references just being an example. The question is, how to create a class that has a generic getXY() method? Which is nicer of the two?


[ March 20, 2007: Message edited by: David Nemeskey ]
I agree. Here's the link:
subject: Class creation with generics
It's not a secret anymore!