我遇到了Swing应用程序的问题,由于我目前无法识别的原因,有时会通过文本输入字段获得焦点。我怀疑某种竞争条件,但我无法看到引起焦点事件的原因。
该字段附加了一个焦点侦听器,因此可以直接向focusGained()事件处理程序添加断点。当我这样做时,我可以看到底层事件,而该事件又包含一个CausedFocusEvent.Cause实例。名称字段设置为“ACTIVATION”。
查看堆栈跟踪,我可以看到以下内容:
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
请注意,InputField是JTextField的子类,其中包含与文本呈现相关的微小更改。
我从堆栈跟踪中可以看出,EDT上的某些内容导致MyPanel中的InputField获得焦点。
我能获得的任何进一步信息能让我了解该组件获得重点的原因吗?
答案 0 :(得分: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);
}
}
(这不是我的解决方案,我不记得我从哪里得到它)
希望我能提供帮助:)
<强>戴夫。强>