loop through all controls on a panel without hardcoding  
Author Message
Adam Sandler





PostPosted: 2007-6-24 13:34:00 Top

java-programmer, loop through all controls on a panel without hardcoding Hello,

In other environments (Delphi and .NET have things like ComponentCount
and FindControl) I've used prior to Java, I could automatically find
all the controls (or only the controls of a certain type, like
checkboxes for example) on a form by passing that form off to some
built in methods which know how to iterate through all the controls
contained within that form.

I haven't been able to find an equivalent feature in Java. In my Java
app, if I have 5 checkboxes on a panel, I would prefer to pass just
the panel to my method for processing (and have checkbox state
determined there) rather than myMethod (cb1, cb2, cb3, cb4, cb5).

How is this done in Java?

Thanks!

 
Andrew Thompson





PostPosted: 2007-6-24 14:23:00 Top

java-programmer >> loop through all controls on a panel without hardcoding Adam Sandler wrote:
.
>...In my Java
>app, if I have 5 checkboxes on a panel, I would prefer to pass just
>the panel to my method for processing (and have checkbox state
>determined there) rather than myMethod (cb1, cb2, cb3, cb4, cb5).

It sounds like a terrible design, but..

>How is this done in Java?

<sscce>
import java.awt.*;
import javax.swing.*;

/**
A starting point for iterating the controls of
a container.
*/
class IterateControls {

public static void printChildren(Container c) {
Component[] children = c.getComponents();
for ( int ii=0; ii<children.length; ii++ ) {
if (children[ii].isFocusable()) {
try {
Container child = (Container)children[ii];
/* if this is also a container, check its
children as well */
printChildren( child );
System.out.println( children[ii].getClass() );
} catch (ClassCastException cce ) {
System.out.println( children[ii] );
}
}
}
}

public static void main(String[] args) {
JPanel mainPanel =
new JPanel(new BorderLayout(5,5));

mainPanel.add( new JScrollPane( new JTree() ),
BorderLayout.WEST );
mainPanel.add( new JLabel("message label"),
BorderLayout.SOUTH );
mainPanel.add( new JScrollPane(
new JTextArea("Output..", 5 ,20) ),
BorderLayout.CENTER );

JPanel checkBoxPanel =
new JPanel( new GridLayout(0,1) );
for (int ii=0; ii<3; ii++) {
checkBoxPanel.add(
new JCheckBox("Checkbox " +
(ii+1), (ii%2==0)) );
}
mainPanel.add( checkBoxPanel, BorderLayout.EAST );
mainPanel.validate();

printChildren( mainPanel );

JOptionPane.showMessageDialog( null, mainPanel );
}
}
</sscce>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

 
Adam Sandler





PostPosted: 2007-6-26 3:22:00 Top

java-programmer >> loop through all controls on a panel without hardcoding On Jun 24, 7:22 am, "Andrew Thompson" <u32984@uwe> wrote:

> It sounds like a terrible design, but..

Thanks for the reply... BTW, I don't write the frameworks. And yeah,
I'm not that enamored with some of the things I see in software
engineering; but I'm learning. Getters and setters are a bad idea too
but that hasn't stopped the rest of the world from polluting their
code with them. I guess that's a rant for another time before I get
too OT. Again, thanks for reply.

 
 
Andrew Thompson





PostPosted: 2007-6-26 17:34:00 Top

java-programmer >> loop through all controls on a panel without hardcoding Adam Sandler wrote:
>> It sounds like a terrible design, but..
>
>Thanks for the reply... BTW, I don't write the frameworks. And yeah,
>I'm not that enamored with some of the things I see in software
>engineering; but I'm learning.

I worked on a project where I was tasked with finding a
particular button on a complex GUI - much like your
question. That is why I could immediately recognise
the shabbyness of the design, knew the answer ..and
can commiserate with you. ;-)

> ...Getters and setters are a bad idea too
>but that hasn't stopped the rest of the world from polluting their
>code with them. I guess that's a rant for another time before I get
>too OT.

In those situations sometimes I just have to hear myself
say it 'out loud' like you did - for my sanity, if nothing else!

> ..Again, thanks for reply.

You're welcome.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-gui/200706/1

 
 
Adam Sandler





PostPosted: 2007-7-3 22:39:00 Top

java-programmer >> loop through all controls on a panel without hardcoding On Jul 3, 2:38 pm, Roedy Green <email***@***.com>
wrote:

>
> It means your client code is terser and easier to decipher.
>

What you write about is certainly provides instant gratification but
what Delphi or Eiffel does (or .NET does with custom properties) isn't
very OO at all! Mutator and accessors methods involve return types;
true objects don't return values, through their messaging they know
where the data lives. In the case of Delphi, where ComponentCount is
used, 99 times out of 100, there is usually an if-then-else hell which
goed along for the ride as the developer has to test for component
type as they iterate though the collection of components on the
form... all very smelly and not better in the long run.

With regard to ease, that's arguable given the complexity of the
project. A programmer who is liberal in their abuse of mutator/
accessors can easily get carried away with classes to hold all of the
offending methods.

What I wrote about earlier is going to be scrapped once the schedule
supports some serious refactoring. I just needed a band aid in the
meantime.