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.
There should be a visual mechanism for setting focus to a component as was implemented in Creator/Visual Web Pack. It is simply a context menu item called "Set Initial Focus". When this item is selected, the component gets a small annotation (a green triangle in the upper left corner), and the menu item is changed to "Clear Initial Focus". In the background, the appropriate call -- component.requestFocusInWindow() -- is added to the code. The justification for this feature is that a well constructed desktop app will set focus to a component when the window is initially displayed. If you do not explicitly set something to receive focus, then chances are it will appear as though nothing has focus, or worse yet, something that shouldn't have focus (like a toolbar button) will have the default focus and an errant keystroke by the user could cause errors. (See the Anagram sample in NB for an example of how this is done).
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) { } } }