Solved

Question about java focus handling...

Posted on 2001-08-21
10
295 Views
Last Modified: 2010-05-18
My predecessor has written the following class.

**********
package FancyAWT;

import java.awt.*;
import java.awt.event.*;
import FancyAWT.Misc;

public class FancyTextField extends TextField implements KeyListener, FocusListener, ActionListener{

    public static final Color       ENABLED_BACKGROUND_COLOR = Color.white;
    public static final Color       ENABLED_FOREGROUND_COLOR = Color.black;
    public static final Color       DISABLED_BACKGROUND_COLOR = new Color(220,220,220);
    public static final Color       DISABLED_FOREGROUND_COLOR = Color.black;

    private boolean                 focusTraversable = true;
    private HandleAwtAction         owner;
    private Color                   foreGround = Color.black;
    private Color                   backGround = Color.white;
    private Font                    font       = Misc.font;


    public FancyTextField(int value, boolean editable, HandleAwtAction parent){
        super(value);

        owner = parent;
     if(editable){
            setForeground(foreGround);
            setBackground(backGround);
     }
     else {
         setForeground(Color.black);
         setBackground(Color.lightGray);
     }
        setFont(font);
            setEditable(editable);
            addActionListener(this);
            addKeyListener(this);
            addFocusListener(this);

        }

     public void setFocusTraversable(boolean value){
         focusTraversable = value;
     }

     public boolean isFocusTraversable(){
         return focusTraversable;
     }

     public void actionPerformed(ActionEvent e){
         owner.handleEvent(e.getSource());
     }

    public void keyPressed(KeyEvent e){
        owner.handleKey(e); // 6/3/2000
    }

    public void keyReleased(KeyEvent e){}

    public void keyTyped(KeyEvent e) {
           }

    public void focusGained(FocusEvent e){
        owner.handleFocus(e.getComponent());
    }

    public void focusLost(FocusEvent e){}

        public void setEditable(boolean b) {
        super.setEditable(b);
        if (b) {
            setForeground(ENABLED_FOREGROUND_COLOR);
            setBackground(ENABLED_BACKGROUND_COLOR);
        }
        else {
            setForeground(DISABLED_FOREGROUND_COLOR);
            setBackground(DISABLED_BACKGROUND_COLOR);
        }
    }
}
***************

when i create a instance of this class by
   FancyTextField ftf = new FancyTe..........
and calling method setFocusTraversable(boolean b) by
   ftf.setFocustraversable(false),

I am able to achieve not sending focus to the parctular field when i press tab key...Really i am not getting how its happening.  I understood by this just i am setting a a boolean value for variable focusTraversable which is declared here itself. What its doing actually, can anybody explain me about this?
0
Comment
Question by:vinaykn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 1

Expert Comment

by:poluru
ID: 6412413
Iam not sure...but,
May be this is implemented in HandleAWTEvent class. When u press the tab key he may be calling ur class's isFocusTraversable(), if it returns true will give focus else not.
0
 
LVL 92

Expert Comment

by:objects
ID: 6412596
The focus manager calls this method (among others) to determine if a component is traversable in the focus cycle.

0
 

Author Comment

by:vinaykn
ID: 6412717
can you explain me clearly. How focus manager is calling this this method?

poluru: Nothing to do with HandleAWTEvent. Its just interface defining somemethond, not at all isFocusTraversable().

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 6412731
The focus manager manages how focus traverses thru your component hierarchy.
Before focus is moved to a component isFocusTransferable() is called to check if this is valid.
0
 

Author Comment

by:vinaykn
ID: 6412869
Yeah.. .  But how here isFocusTraversable() returning false, just by my setting my boolean variable whichis named 'focusTraversable' in my programme, is false.
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6415764
Sorry I'm a bit unsure what you're confused about.
You call setFocusTransferable(false) which sets the member variable focusTransferable to false. So when you hit <Tab> and the focus manager looks for the next component in the focus cycle isFocusTraversable() for your component returns false so the focus skips your component.

    public void setFocusTraversable(boolean value){
        focusTraversable = value;
    }

    public boolean isFocusTraversable(){
        return focusTraversable;
    }
   
0
 

Author Comment

by:vinaykn
ID: 6416392
OH! You mean, focus manager will call the method isFocusTraversable() and basing on that returned value..it goes to next component.

I am not aware that it will call isFocusTrversable() method. Now i think i am clear.

Please confirm it what i am guessing from ur words, and take points for this . :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 6416409
Yes that is correct.
0
 

Author Comment

by:vinaykn
ID: 6416442
Thanks once again for enlighten me. I believe yu sure continue in helping me..

:-):-) :-):-)
0
 
LVL 92

Expert Comment

by:objects
ID: 6427402
No worries.
Until next time :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 108
spring jars download 1 52
Android development question 2 78
Selenium docs api java index 3 66
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question