Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.gui > #1888
| From | "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> |
|---|---|
| Subject | Re: loop through all cont |
| Message-ID | <742a248e4f099@uwe> (permalink) |
| Newsgroups | comp.lang.java.gui |
| References | <1182663216.188722.72430@i38g2000prf.googlegroups.com> |
| Date | 2011-04-27 15:35 +0000 |
| Organization | TDS.net |
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
Back to comp.lang.java.gui | Previous | Next — Previous in thread | Next in thread | Find similar
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
csiph-web