Results 1 to 6 of 6
- 03-07-2012, 04:51 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Preventing component focus on window launch
I've added a focus listener to a component.
The problem I have is that the listener fires as soon as the app launches, because on launch a component will get focus.
I want to prevent that.
Any suggestions?
Sample..
I don't want to see "Focus Gained" until the user clicks on the component.Java Code:addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e) { System.out.println("Focus Gained"); } });
I've been looking at java.awt.Window.processWindowFocusEvent(WindowEven t e); But it's protected.
- 03-07-2012, 07:37 AM #2
Re: Preventing component focus on window launch
Simple logic. Declare an instance field of type boolean, and in the listener code, first test the field, process other logic only if true, then set the field to true.
An alternative approach could be to ensure that the particular component isn't the focused one wehn the application starts. You could use a focus traversal policy for that -- see the Oracle tutorial on the focus subsystem.
Yet another way, since you mentioned a mouse click: if you don't want the component to initially receive focus via the keyboard, you could setFocusable(false) initially, and set it to true in a mousePressed event.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-07-2012, 02:05 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: Preventing component focus on window launch
db,
Thanks. These appear to all be work-arounds that have unwanted side effects.
Any idea where the event comes from? Could I extend another class to stop it from happening? or prevent it from reaching my component? I'd prefer to not have any component get focus at all, so deflecting focus away from one component actually gives it to another.
- 03-07-2012, 06:49 PM #4
Re: Preventing component focus on window launch
If you need more suggestions, you're going to have to describe your requirement better. Note: what you need and why, not how you expect to achieve it.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-07-2012, 09:06 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 3
- Rep Power
- 0
Re: Preventing component focus on window launch
My whole app is a form.
I've extended JTextField and added a feature to set an inner text label. On focus it needs to disappear.
But I don't want to lose my inner label for any of my fields when the app launches.
Appreciate the replies.Java Code:addFocusListener(new FocusListener(){ public void focusGained(FocusEvent e) { System.out.println("Focus Gained"); if(!_hasInnerLabel){ setText(""); setForeground(_userColor); } } public void focusLost(FocusEvent e) { if(getText().equals("")){ setText(_innerLabel); setForeground(_innerLabelColor); _hasInnerLabel = false; }else{ _hasInnerLabel = true; } } })
- 03-08-2012, 06:00 AM #6
Re: Preventing component focus on window launch
You may find camickr's Text Prompt useful.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
focus on a specific component
By bigjo in forum AWT / SwingReplies: 9Last Post: 11-26-2011, 09:46 PM -
Window Focus
By collin389 in forum New To JavaReplies: 4Last Post: 12-29-2009, 03:03 AM -
Loosing TableCellEditor Component Focus
By sandeepsai39 in forum AWT / SwingReplies: 0Last Post: 03-24-2009, 06:39 AM -
Loosing TableCellEditor Component Focus
By sandeepsai39 in forum New To JavaReplies: 0Last Post: 03-23-2009, 05:42 AM -
How to Focus Next Component Sample
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:18 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks