Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #1887 > unrolled thread

loop through all controls

Started by"Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this>
First post2011-04-27 15:35 +0000
Last post2011-04-27 15:36 +0000
Articles 6 — 3 participants

Back to article view | Back to comp.lang.java.gui


Contents

  loop through all controls "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
    Re: loop through all cont "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
      Re: loop through all cont "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
        Re: loop through all cont "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:35 +0000
        Re: loop through all cont "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000
          Re: loop through all cont "Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this> - 2011-04-27 15:36 +0000

#1887 — loop through all controls

From"Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
Subjectloop through all controls
Message-ID<1182663216.188722.72430@i38g2000prf.googlegroups.com>
  To: comp.lang.java.gui
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!

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#1888 — Re: loop through all cont

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: loop through all cont
Message-ID<742a248e4f099@uwe>
In reply to#1887
  To: comp.lang.java.gui
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

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1894 — Re: loop through all cont

From"Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: loop through all cont
Message-ID<1182799310.529662.149620@n2g2000hse.googlegroups.com>
In reply to#1888
  To: comp.lang.java.gui
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.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1895 — Re: loop through all cont

From"Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this>
Date2011-04-27 15:35 +0000
SubjectRe: loop through all cont
Message-ID<7444f51de1a15@uwe>
In reply to#1894
  To: comp.lang.java.gui
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

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1939 — Re: loop through all cont

From"Roedy Green" <roedy.green@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
SubjectRe: loop through all cont
Message-ID<f5kk831b0oalfokptf872j2fsnlovu7vur@4ax.com>
In reply to#1894
  To: comp.lang.java.gui
On Mon, 25 Jun 2007 12:21:50 -0700, Adam Sandler <corn29@excite.com>
wrote, quoted or indirectly quoted someone who said :

>  Getters and setters are a bad idea too
I like the Delphi or Eiffel idea of using the same syntax for invoking
getters and setters and you do for accessing the variables directly.

That means you can add a REAL  getter or setter later without
disturbing all your clients.  Sometimes you dare not refactor since it
would infuriate all the programmers with client software.

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

It means you don't litter your code without dummy getters and setters,
only ones that actually do something useful.

This is one of my Bali proposals for Java. See
http://mindprod.com/jgloss/bali.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [next] | [standalone]


#1940 — Re: loop through all cont

From"Adam Sandler" <adam.sandler@THRWHITE.remove-dii-this>
Date2011-04-27 15:36 +0000
SubjectRe: loop through all cont
Message-ID<1183473549.174123.137390@n2g2000hse.googlegroups.com>
In reply to#1939
  To: comp.lang.java.gui
On Jul 3, 2:38 pm, Roedy Green <see_webs...@mindprod.com.invalid>
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.

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web