java swing to count all controls

前端 未结 3 663
-上瘾入骨i
-上瘾入骨i 2021-01-27 10:59

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 o

相关标签:
3条回答
  • 2021-01-27 11:10

    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

    0 讨论(0)
  • 2021-01-27 11:21

    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.

    0 讨论(0)
  • 2021-01-27 11:23

    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();
    
    0 讨论(0)
提交回复
热议问题