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
Solved

Question about java focus handling...

Posted on 2001-08-21
10
294 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
Groovy problem when using SOAPUI : DispatchException occurred 7 58
arguments to jar 5 35
mysql jsp example issue 32 48
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

861 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