This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
Summary: | Set Initial Focus feature for Swing form editing | ||
---|---|---|---|
Product: | guibuilder | Reporter: | jhoffman <jhoffman> |
Component: | Code | Assignee: | issues@guibuilder <issues> |
Status: | NEW --- | ||
Severity: | blocker | CC: | gtzabari |
Priority: | P2 | Keywords: | UI |
Version: | 5.x | ||
Hardware: | All | ||
OS: | All | ||
Issue Type: | ENHANCEMENT | Exception Reporter: |
Description
jhoffman
2006-12-21 22:13:12 UTC
Some related issues are 75811, 81626 (in mobility) and 68802 (in form) Agree this is a useful enhancement. Just the implementation is a bit trickier: component.requestFocusInWindow() in initComponents() only works if the component is directly in the dialog or frame, but not if it is in a panel that is added to some window later. Here is a simple implementation that will work for any Component, regardless of whether it is directly inside a Window or not: class MyComponent extends JPanel { public MyComponent() { addHierarchyListener(new SetInitialFocus()); } /** * Sets the keyboard focus when the first time the panel becomes visible. */ private class SetInitialFocus implements HierarchyListener, WindowFocusListener { @Override public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) != 0 && getParent() != null) { SwingUtilities.getWindowAncestor(MyComponent.this).addWindowFocusListener(this); removeHierarchyListener(this); } } @Override public void windowGainedFocus(WindowEvent e) { foobar.requestFocusInWindow(); // ** Magic happens here ** e.getWindow().removeWindowFocusListener(this); } @Override public void windowLostFocus(WindowEvent e) { } } } |