Solved

Question about java focus handling...

Posted on 2001-08-21
10
291 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 103
JAVA part two 5 58
maven example error 3 54
servlet web applications   metadata-complete="true" or false 3 6
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now