🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #12791

Re: JFrame Default focus Problem

#12791
From: Max Polk
Date: Mon, 16 Nov 1998 00:00
25 lines
1866 bytes
In article <3650BACC.5365258@ultranet.com>, apogee08@ultranet.com says...
> I have a JFrame with a couple of JTextField's on it. I want a particular
> JTextField to have focus, a flashing caret and be ready for user input
> the moment it becomes visible. I can get it to have focus and a flashing
> caret, but the user must always click in the control or tab out and back
> into it to be able to actually begin entering text.
> 
> I've tried getDefaultFocus(), grabFocus(), requestFocus() and I've even
> created my own Focus and MouseEvents and done a dispatchEvent...nothing
> seems to work. This is using Swing 1.0.3, the problem seems even worse
> with Swing 1.0.1. Any help would be greatly appreciated.

The problem in most cases is solved by adding a focus listener to *THE 
RIGHT COMPONENTS* and when the component gets focus (typically a frame or 
window or panel), set the focus to the desired component within it.

The trick is not programmatically calling requestFocus when the component 
is created, but rather, *WAIT* until a focus event occurs, which can be 
much, much later in time when the GUI is finally pretty far along 
painting, and when you get it, *THAT* is the time to set the focus on 
your desired component.

Also make sure to never break the "single thread rule".  See the swing 
connection on java.sun.com for more information about why Swing is *NOT* 
thread safe.
Technical Details
Message-ID: <MPG.10ba70427f8c380a9896bd@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3650BACC.5365258@ultranet.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,9878af0d635aaa30
X-Google-Attributes: gid111d45,public
From: none@none.com (Max Polk)
Subject: Re: JFrame Default focus Problem
Date: 1998/11/16
Message-ID: <MPG.10ba70427f8c380a9896bd@forums.borland.com>
X-Deja-AN: 412546174
References: <3650BACC.5365258@ultranet.com>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing