2012-03-16 10 views
1

クラス内のコンポーネントのリストとそれぞれの変数名を持つことができる必要があります。私はGUIのすべてのコンポーネントのリストを取得することができました。しかし、コンポーネントのgetNameメソッドを呼び出すと、nullが返されます。誰もコンポーネントのためにそのような名前を得ることができる方法を知っていますか?JavaでGUIコンポーネントの名前を取得するには?

これは、これまでのコードです:コンポーネントの

public static void main(String[] args){ 
    Calculator c = new Calculator(); 

    List<Component> containers = getAllComponents(c.getContentPane()); 
    for (int i = 0; i < containers.size(); i++) { 
     System.out.println(containers.get(i).getClass().getName()); 
     System.out.println(containers.get(i).getName()); 
    } 
    System.exit(0); 
} 

public static List<Component> getAllComponents(final Container c) { 
    Component[] comps = c.getComponents(); 
    List<Component> compList = new ArrayList<Component>(); 
    for (Component comp : comps) { 
     compList.add(comp); 
     if (comp instanceof Container) { 
      compList.addAll(getAllComponents((Container) comp)); 
     } 
    } 
    return compList; 
} 

答えて

1

getName()あなたが前にsetName()を呼び出した場合にのみ値を返します。しかし、その名前は任意です。これは、UI開発者のためのデバッグ支援ツールです。

私の推測では、どのコンポーネントがCalculatorクラスのどのフィールドに属しているか知りたいと思っています。このためにはReflection APIを使用する必要があります。

お試しCalculator.class.getDeclaredFields()プライベートフィールドの値を読み取るには、this example codeを参照してください。

+0

変数の名前と型を取得できました...私はこの方法で実装を続けることができると思います。 – ict1991

1

getName()方法は、値を返すことが保証されていない、と確かにコンポーネントがあまりに割り当てられた変数名を返しません。また、いくつかの変数が同じオブジェクトインスタンスを指している可能性があるので、情報が利用可能であっても有用ではありません。

+0

したがって、コンポネントの変数名を取得する方法はありませんか? – ict1991

+0

あなたのコードで静的解析を実行することはできませんが、かなり根本的なステップは私が確信していることは簡単な要件です。あなたは何を達成しようとしていますか? – Perception

+0

私はツールを構築しており、最終的に私は強調する必要のあるコンポーネントを特定する必要があります。だから私は実際のコンポーネントと変数の名前が必要です。最初はgetXとgetYを使用してコンポーネントの位置を特定しようとしましたが、ゼロが返されました。 – ict1991

0

各コンポーネントには、getName()およびsetName()でアクセスする名前を付けることができますが、独自のルックアップ機能を作成する必要があります。

ハッシュマップで試すこともできます。私はその事例を見つけました。

Create a Map class variable. You'll need to import HashMap at the very least. I named mine componentMap for simplicity. 

private HashMap componentMap; 

    Add all of your components to the frame as normal. 

initialize() { 
    //add your components and be sure 
    //to name them. 
    ... 
    //after adding all the components, 
    //call this method we're about to create. 
    createComponentMap(); 
} 

    Define the following two methods in your class. You'll need to import Component if you haven't already: 

private void createComponentMap() { 
     componentMap = new HashMap<String,Component>(); 
     Component[] components = yourForm.getContentPane().getComponents(); 
     for (int i=0; i < components.length; i++) { 
       componentMap.put(components[i].getName(), components[i]); 
     } 
} 

public Component getComponentByName(String name) { 
     if (componentMap.containsKey(name)) { 
       return (Component) componentMap.get(name); 
     } 
     else return null; 
} 

    Now you've got a HashMap that maps all the currently existing components in your frame/content pane/panel/etc to their respective names. 

    To now access these components, it is as simple as a call to getComponentByName(String name). If a component with that name exists, it will return that component. If not, it returns null. It is your responsibility to cast the component to the proper type. I suggest using instanceof to be sure. 
+0

iveがコードを追加しました – ict1991

関連する問題