Solved

Question about java focus handling...

Posted on 2001-08-21
10
297 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

624 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