Java tutorial
//package com.java2s; //License from project: Open Source License import javax.swing.*; import java.awt.*; import java.util.ArrayList; import java.util.List; public class Main { public static JButton getButton(Container container, String text) { JButton btn = null; List<Container> children = new ArrayList<>(25); for (Component child : container.getComponents()) { if (child instanceof JButton) { JButton button = (JButton) child; if (text.equals(button.getText())) { btn = button; break; } } else if (child instanceof Container) { children.add((Container) child); } } if (btn == null) { for (Container cont : children) { JButton button = getButton(cont, text); if (button != null) { btn = button; break; } } } return btn; } }