Article View: borland.public.jbuilder.java.language
Article #14679Re: hiding methods
From: Paul Furbacher
Date: Thu, 12 Aug 1999 00:00
Date: Thu, 12 Aug 1999 00:00
65 lines
3324 bytes
3324 bytes
>>i am creating a class that extends Panel and has antoher panel and
>>scrollbar laid out with the borderlayout. i want the users of this class
>>to be unable to add other components, change the layout and other
>>things. does anyone have a clue on how to achieve this, or is this
>>inpossible for oop reasons?
Turns out that I'm just now finishing up a complex bean
which is based on BevelPanel, so I tried some approaches.
The first is valid if someone is only going to use your bean
within JBuilder. If you "implement borland.jbcl.util.BlackBox"
this will tell the Designer that it shouldn't allow the user
to add components to this component.
That obviously has limitations.
Dillan Fernando suggested:
> The java.awt.Container has a method called addImpl(..),
> This takes three parameters (addImpl(Component comp,
> Object constraints, int index)
>
> When someone adds any component, this method is called
> automatically. So you can overide this method to check
> what components are dropped onto you panel.
Actually, I think the preferable method to override is the
polymorphic "add(...)" so that it does nothing. There are
five of them. Also, you could override other container altering
methods, such as setLayout() to do nothing.
If you do this, you are left with a slight problem. Your JavaBean
obviously must add its own components to itself, but you've just
overridden the add() method so that it does nothing. Well,
just edit every occurrence of "this.add(...)" in your JavaBean's
"jbInit()" method to read "super.add(...)". That does it.
Funny thing is that the Designer will seem to allow you to add
the component, but if you investigate the component composition
of your JavaBean instance at runtime, you'll see that nothing has
been added. Use this short method to prove it to yourself:
void iterateContainer(Container container, int indentLevel) {
borland.util.FastStringBuffer buffer = new FastStringBuffer(100);
for ( int i = 0; i < indentLevel * 2; i++ )
buffer.append(" ");
Component [] components = container.getComponents();
for ( int i = 0; i < components.length; i++ ) {
Component c = components[i];
System.out.println(buffer.toString() + "component: " + c.toString());
if ( c instanceof Container )
iterateContainer((Container)c, indentLevel + 1);
}
}
To investigate an instance, "myBean", of your bean, just call
iterateContainer(myBean, 1);
Drop a button on your bean and give it a really long name.
It will be easier to find it in the long list if it's actually there.
If you've done it right, it won't be!
Paul Furbacher (TeamB)
Technical Details
Message-ID:
<37B3600F.476084E2@bilbo.bio.purdue.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<37A94A3C.7124DC62@c-content.nl> <7onju2$8ce10@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,c7695f50bddab9ba X-Google-Attributes: gidf74b3,public From: Paul Furbacher <pf@bilbo.bio.purdue.edu> Subject: Re: hiding methods Date: 1999/08/12 Message-ID: <37B3600F.476084E2@bilbo.bio.purdue.edu> X-Deja-AN: 512055788 Content-Transfer-Encoding: 7bit References: <37A94A3C.7124DC62@c-content.nl> <7onju2$8ce10@forums.borland.com> Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pf@bilbo.bio.purdue.edu Newsgroups: borland.public.jbuilder.java.language