code_junkie

Java JFrame: howto parse recursively through componets (i.e. if using JScrollPane)

Nov 12th, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. public Component getComponent(String componentName) {
  2. return getComponent(componentName, this.frame.getContentPane());
  3. }
  4.  
  5. private Component getComponent(String componentName, Component component) {
  6. Component found = null;
  7.  
  8. if (component.getName() != null && component.getName().equals(componentName)) {
  9. found = component;
  10. } else {
  11. for (Component child : ((Container) component).getComponents()) {
  12. found = getComponent(componentName, child);
  13.  
  14. if (found != null)
  15. break;
  16. }
  17. }
  18. return found;
  19. }
  20.  
  21. JTextArea ta = new JTextArea();
  22. ta.setName("fooburg");
  23. JScrollPane scr = new JScrollPane(ta);
  24. frame.getContentPane().add(scr, "1, 1"); // EDIT: changed, tkx bruno
  25.  
  26. void printNames(Component component) {
  27. System.out.println(component.getName());
  28. for (Component child : component.getComponents()) {
  29. printNames(child);
  30. }
  31. }
  32.  
  33. void printFrameNamesComponents(JFrame frame) {
  34. printNames(frame.getContentPane());
  35. }
  36.  
  37. frame.getContentPane().add(comp);
Add Comment
Please, Sign In to add comment