Results 1 to 6 of 6
  1. #1
    ericusername is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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..

    Java Code:
    		addFocusListener(new FocusListener(){
    		    public void focusGained(FocusEvent e) {
    		    	System.out.println("Focus Gained");
    		    }
              });
    I don't want to see "Focus Gained" until the user clicks on the component.

    I've been looking at java.awt.Window.processWindowFocusEvent(WindowEven t e); But it's protected.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    ericusername is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    ericusername is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default 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.

    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;
    		    	}
    
    		    }
    			
    		})
    Appreciate the replies.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Preventing component focus on window launch

    You may find camickr's Text Prompt useful.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. focus on a specific component
    By bigjo in forum AWT / Swing
    Replies: 9
    Last Post: 11-26-2011, 09:46 PM
  2. Window Focus
    By collin389 in forum New To Java
    Replies: 4
    Last Post: 12-29-2009, 03:03 AM
  3. Loosing TableCellEditor Component Focus
    By sandeepsai39 in forum AWT / Swing
    Replies: 0
    Last Post: 03-24-2009, 06:39 AM
  4. Loosing TableCellEditor Component Focus
    By sandeepsai39 in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 05:42 AM
  5. How to Focus Next Component Sample
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •