java swing to count all controls

99封情书 提交于 2019-12-20 05:40:33

问题


How to count number of controls like JTextField, J Label and so on so..in java swing form designing,for example if we use only one textbox and one text field means i need the output as follows count value is 2.how to do this?


回答1:


The container for the controls contains a list of controls, see Container.getComponents(). Come to think, it also has a method for the count of the controls, if that's all you want, Container.getComponentCount().

If you want this for all the controls contained within containers contained (ultimately) in a frame, you will need to look at each one recursively; start with the frame, and for each component that is a container, get the controls/count in that container, etc. Keep in mind that panels can have panels.

rc




回答2:


As they should all be inside your Container object you can use the getComponentCount() method to return you an int of the number of components.

Container c;
int count;
count = c.getComponentCount();



回答3:


With SwingX, you have in class org.jdesktop.swingx.util.WindowUtils public static List<Component> getAllComponents(Container c). With that you have all components and subcomponents in container.



来源:https://stackoverflow.com/questions/8556982/java-swing-to-count-all-controls

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!