Solved

Question about java focus handling...

Posted on 2001-08-21
10
288 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

22 Experts available now in Live!

Get 1:1 Help Now