1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 17-Jun-2006
* Time: 20:15:38
* To change this template use File | Settings | File Templates.
*/
public class ListElements extends JFrame {
Random r = new Random();
public ListElements() throws HeadlessException {
JToolBar[] toolBars = new JToolBar[10];
int orientation = 0;
for (int i = 0; i < 10; i++) {
toolBars[i] = new JToolBar(orientation++ % 2);
JComponent aComponent = null;
int rnd;
for (int j = 0; j < 3; j++) {
rnd = r.nextInt(5);
System.out.println(rnd);
switch (rnd) {
case 0:
aComponent = new JButton(String.valueOf(i));
break;
case 1:
aComponent = new JLabel(String.valueOf(i));
break;
case 2:
aComponent = new JComboBox(new Object[]{i});
break;
case 3:
aComponent = new JTextField(String.valueOf(i));
break;
case 4:
aComponent = new JSpinner();
break;
}
toolBars[i].add(aComponent);
}
}
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = GridBagConstraints.REMAINDER;
for (int i = 0; i < 10; i++) {
add(toolBars[i], gbc);
}
}
public Collection<Component> getComponents(String className, Container aContainer) {
if (className == null || aContainer == null) {
throw new IllegalArgumentException("I need a className to do my job correctly...");
}
ArrayList<Component> a = new ArrayList<Component>();
Component[] components = aContainer.getComponents();
for (Component c : components) {
if (c != null) {
if (className.equals(c.getClass().getName())) {
a.add(c);
} else if (c instanceof Container) {
Container aSubContainer = (Container) c;
a.addAll(getComponents(className, aSubContainer));
}
}
}
return a;
}
public static void print(Collection<Component> c) {
if (c.size() == 0) {
System.out.println("empty collection");
return;
}
for (Object o : c) {
System.out.println(o);
}
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
ListElements l = new ListElements();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setSize(540, 400);
l.setLocationRelativeTo(null);
l.setVisible(true);
Collection<Component> c = l.getComponents("javax.swing.JToolBar", l.getRootPane());
print(c);
c = l.getComponents("javax.swing.JButton", l.getRootPane());
print(c);
c = l.getComponents("javax.swing.JLabel", l.getRootPane());
print(c);
c = l.getComponents("javax.swing.JComboBox", l.getRootPane());
print(c);
c = l.getComponents("javax.swing.JMyComponent", l.getRootPane());
print(c);
}
};
SwingUtilities.invokeLater(runnable);
}
} |
Partager