Swingでコンポーネントを列挙する(改訂2)

Swingのウィンドウ列挙関数を改訂した。
JMenuの子ウィンドウはJComponent.getComponentでは取得できない。
どうやらクラス毎に特殊な子ウィンドウ取得が必要のようだ。

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 : getComponents(root)) {
		if(c instanceof JComponent) {
			list.add((JComponent)c);
			collectComponets((JComponent)c, list);
		}
	}
}
Component[] getComponents(JComponent root) {
	if(root instanceof JMenu) {
		return ((JMenu)root).getMenuComponents();
	} else {
		return root.getComponents();
	}
}

コメントを残す

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