Swingでコンポーネントを列挙する

Windowsでウィンドウを列挙するのは簡単だ。
だけどSwingで手軽にウィンドウを列挙する方法がどうも見当たらない。
仕方ないのでJComponentに限定するウィンドウ列挙関数を自作してみた。
SwingのJComponentがAWTのContainerを基底クラスにしていることを利用している。

Vector<JComponent> collcetApplicationJComponents() {
	Vector<JComponent> list = new Vector<JComponent>();
	for(Frame f : Frame.getFrames()) {
		if(f instanceof JFrame) {
			collectComponets(((JFrame)f).getRootPane(), list);
		}
	}
	return list;
}
void collectComponets(JComponent root,
                                 Vector<JComponent> list) {
	for(Component c : root.getComponents()) {
		if(c instanceof JComponent) {
			list.add((JComponent)c);
			collectComponets((JComponent)c, list);
		}
	}
}

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です