How to get all elements inside a JFrame?

后端 未结 4 1398
悲哀的现实
悲哀的现实 2020-11-28 10:52

I have this code to get all the elements I need and do some processing. The problem is I need to specify every panel I have to get the elements inside it.

fo         


        
4条回答
  •  执笔经年
    2020-11-28 11:25

                for (Component c : mainPanel.getComponents()) {
                    for (Component sc : ((JPanel) c).getComponents()) {
                        if (sc instanceof JTextField) {
                            //process
                        }
                    }
                }
    

    in my code, im just getting all instances of jtextfield. u can use same logic. this is just example of getting all sub-components from components you have taken. Hope it will help u out.

提交回复
热议问题