2013-07-24 9 views
5

私はSwingアプリケーションに問題があり、現在は識別できない理由で、テキスト入力フィールドでフォーカスが得られることがあります。私は何らかの競合状態を疑うが、フォーカスイベントの原因を知ることはできない。Java Swing UIでのフォーカス要求の起点の特定

フィールドにはフォーカスリスナーが関連付けられているため、フォーカスをフォーカスするイベントハンドラにブレークポイントを追加するのは簡単です。私が行ったとき、私はCausedFocusEvent.Causeのインスタンスを含む基礎イベントを見ることができます。名前フィールドは「ACTIVATION」に設定されています。スタックトレースを見ると

、私は以下を参照してくださいすることができますのInputFieldは、テキストのレンダリングに関連する軽微な変更であることのJTextFieldのサブクラスである

Thread [AWT-EventQueue-0] (Suspended (breakpoint at line 174 in MyPanel$3)) 
    MyPanel$3.focusGained(FocusEvent) line: 174 
    AWTEventMulticaster.focusGained(FocusEvent) line: not available 
    InputField(Component).processFocusEvent(FocusEvent) line: not available 
    InputField(Component).processEvent(AWTEvent) line: not available  
    InputField(Container).processEvent(AWTEvent) line: not available  
    InputField(Component).dispatchEventImpl(AWTEvent) line: not available 
    InputField(Container).dispatchEventImpl(AWTEvent) line: not available 
    InputField(Component).dispatchEvent(AWTEvent) line: not available 
    DefaultKeyboardFocusManager(KeyboardFocusManager).redispatchEvent(Component, AWTEvent) line: not available 
    DefaultKeyboardFocusManager.typeAheadAssertions(Component, AWTEvent) line: not available  
    DefaultKeyboardFocusManager.dispatchEvent(AWTEvent) line: not available 
    InputField(Component).dispatchEventImpl(AWTEvent) line: not available 
    InputField(Container).dispatchEventImpl(AWTEvent) line: not available 
    InputField(Component).dispatchEvent(AWTEvent) line: not available 
    SunToolkit$1.run() line: not available 
    PeerEvent(InvocationEvent).dispatch() line: not available 
    EventQueue.dispatchEventImpl(AWTEvent, Object) line: not available 
    EventQueue.access$200(EventQueue, AWTEvent, Object) line: not available 
    EventQueue$3.run() line: not available 
    EventQueue$3.run() line: not available 
    AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]  
    ProtectionDomain$1.doIntersectionPrivilege(PrivilegedAction<T>, AccessControlContext, AccessControlContext) line: not available 
    EventQueue.dispatchEvent(AWTEvent) line: not available 
    EventDispatchThread.pumpOneEventForFilters(int) line: not available 
    EventDispatchThread.pumpEventsForFilter(int, Conditional, EventFilter) line: not available 
    EventDispatchThread.pumpEventsForHierarchy(int, Conditional, Component) line: not available 
    EventDispatchThread.pumpEvents(int, Conditional) line: not available  
    EventDispatchThread.pumpEvents(Conditional) line: not available 
    EventDispatchThread.run() line: not available 

注意を。

私がスタックトレースから知ることは、EDT上の何かがMyPanelのInputFieldにフォーカスを当てていることです。

このコンポーネントでフォーカスが得られた理由を私に見てもらえる情報はありますか?

+1

すべてのSwingイベントは単一スレッドEDTで実行する必要があるため、競合状態は発生しません。問題が解消するまでコードを切り捨て、問題を再現する最小限のコードを見つけようとしてから、ここにコードを投稿して、問題の原因となるコードを切り離すことを検討してください。不思議そうなこと:FocusListenerを何のために使用していますか? –

+1

私はシングルスレッドについて同意しますが、私の懸念は、スイングが基本的なレンダリングを非決定論的な順序で実行している可能性があり、何かがその順序に不注意に依存していることです。フォーカスリスナーは主にデバッグのためのものであり、フォーカスが得られたときに影響を与えることはなく、ただ反応します。 – PhilDin

+1

'は、EDT上の何かが、MyPanelのInputFieldがフォーカスを獲得した原因です。???より良いヘルプのために、早くSSCCEをポストしてください。ショート、実行可能、コンパイル可能、focusGainedの原因はInputFieldからJVM例外によってリダイレクトされるto MyPanel – mKorbel

答えて

0

私はちょうどフォーカス階層を指定し、Javaから注目を超えるすべてのコントロールをした最後に、同様の問題、

を持っていました。 それは、このような少し何か行く:

// Setting The FOCUS Order. 
    Vector<Component> order = new Vector<Component>(7); 
    order.add(projectNameJTextField); 
    order.add(companyJTextField); 
    order.add(orderedByJTextField); 
    order.add(phoneJTextField); 
    order.add(jRadioNormalPriority); 
    order.add(jRadioHighPriority); 
    order.add(jCheckBox1); 
    order.add(jCheckBox2); 
    order.add(jCheckBox3); 
    order.add(jCheckBox4); 
    order.add(jCheckBox5); 
    order.add(jCheckBox6); 
    order.add(textDetailJTextArea); 
    order.add(technitianJComboBox); 
    order.add(aproxTimeJTextField); 
    order.add(sendButton); 

    // Sending the costume focus order to the focus Policy Class, 
    MyOwnFocusTraversalPolicy newPolicy = new MyOwnFocusTraversalPolicy(order); 
    setFocusTraversalPolicy(newPolicy); 

を、あなたも、このクラスが必要になります。

import java.awt.Component; 
    import java.awt.Container; 
    import java.awt.FocusTraversalPolicy; 
    import java.util.Vector; 



    // This Class Helps to Set A Costume FOCUS Order. 
    public class MyOwnFocusTraversalPolicy extends FocusTraversalPolicy 
    { 
    Vector<Component> order; 

    public MyOwnFocusTraversalPolicy(Vector<Component> order) { 
     this.order = new Vector<Component>(order.size()); 
     this.order.addAll(order); 
    } 
    public Component getComponentAfter(Container focusCycleRoot, 
             Component aComponent) 
    { 
     int idx = (order.indexOf(aComponent) + 1) % order.size(); 
     return order.get(idx); 
    } 

    public Component getComponentBefore(Container focusCycleRoot, 
             Component aComponent) 
    { 
     int idx = order.indexOf(aComponent) - 1; 
     if (idx < 0) { 
      idx = order.size() - 1; 
     } 
     return order.get(idx); 
    } 

    public Component getDefaultComponent(Container focusCycleRoot) { 
     return order.get(0); 
    } 

    public Component getLastComponent(Container focusCycleRoot) { 
     return order.lastElement(); 
    } 

    public Component getFirstComponent(Container focusCycleRoot) { 
     return order.get(0); 
    } 
    } 

(これは私の解決策ではない、と私はからそれを得たところ、私が覚えてカント)

希望私は助けることができる:)

Dave。

+0

_「どこから取得したのかわからない」[チュートリアルの例](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/) .com/javase/tutorial/uiswing/examples/misc/FocusTraversalDemoProject/src/misc/FocusTraversalDemo.java);-) –

+0

右:ありがとうございました:) –

関連する問題