Solved

Focus Problem!! using setNextFocusableComponent

Posted on 2000-03-14
3
399 Views
Last Modified: 2012-05-04
Hello,
i want to know how the setNextFocusableComponent works...
Suppose in my gui i have four JTextFields placed in the order as below

TextField1      TextField2
TextField3  TextField4

I want some thing like this..
1. When the program loads The focus should be on TextField2
2 on tab it should go to TextField1
3.Again on tab it should go to TextField4
4.Again on tab it should go to TextField3
5.Again on tab it should go to TextField2

i tried overriding isManagingFocus (return false)but it doesn't seem to be working properly.. i am also getting an exception..
*************************Exception*********************
Exception occurred during event dispatching:
java.lang.ArrayIndexOutOfBoundsException: 4 > 3
      java.lang.Throwable(java.lang.String)
      java.lang.Exception(java.lang.String)
      java.lang.RuntimeException(java.lang.String)
      java.lang.IndexOutOfBoundsException(java.lang.String)
      java.lang.ArrayIndexOutOfBoundsException(java.lang.String)
      void java.util.Vector.insertElementAt(java.lang.Object, int)
      java.awt.Component [] com.sun.java.swing.DefaultFocusManager.childrenTabOrder(java.awt.Container)
      java.awt.Component com.sun.java.swing.DefaultFocusManager.getComponentAfter(java.awt.Container, java.awt.Component)
      java.awt.Component com.sun.java.swing.DefaultFocusManager.getNextComponent(java.awt.Component, java.awt.Container, boolean)
      java.awt.Component com.sun.java.swing.DefaultFocusManager.getFocusableComponentAfter(java.awt.Component, java.awt.Container, boolean)
      void com.sun.java.swing.DefaultFocusManager.focusNextComponent(java.awt.Component)
      void com.sun.java.swing.DefaultFocusManager.processKeyEvent(java.awt.Component, java.awt.event.KeyEvent)
      void com.sun.java.swing.JComponent.processKeyEvent(java.awt.event.KeyEvent)
      void java.awt.Component.processEvent(java.awt.AWTEvent)
      void java.awt.Container.processEvent(java.awt.AWTEvent)
      void java.awt.Component.dispatchEventImpl(java.awt.AWTEvent)
      void java.awt.Container.dispatchEventImpl(java.awt.AWTEvent)
      void java.awt.Component.dispatchEvent(java.awt.AWTEvent)
      boolean java.awt.LightweightDispatcher.processKeyEvent(java.awt.event.KeyEvent)
      boolean java.awt.LightweightDispatcher.dispatchEvent(java.awt.AWTEvent)
      void java.awt.Container.dispatchEventImpl(java.awt.AWTEvent)
      void java.awt.Window.dispatchEventImpl(java.awt.AWTEvent)
      void java.awt.Component.dispatchEvent(java.awt.AWTEvent)
      void java.awt.EventDispatchThread.run()

************************************End****************************************

Please help me with this..
Thanx
Priyan.T
priyan.thiruthiyil@lisec.com
ICQ:54490947

0
Comment
Question by:Priyan
3 Comments
 
LVL 8

Accepted Solution

by:
diakov earned 50 total points
ID: 2615180
I have put up a quick example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyFrame extends JFrame
{
  BorderLayout borderLayout1 = new BorderLayout();
  JTextField jTextField1 = new JTextField();
  JTextField jTextField2 = new JTextField();
  JTextField jTextField3 = new JTextField();
  JTextField jTextField4 = new JTextField();
  JTextField jTextField5 = new JTextField();

  //Construct the frame
  public MyFrame()
  {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try
    {
      jbInit();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception
  {
    jTextField1.setText("jTextField1");
    this.getContentPane().setLayout(borderLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Test");
    jTextField2.setText("jTextField2");
    jTextField3.setText("jTextField3");
    jTextField4.setText("jTextField4");
    jTextField5.setText("jTextField5");
    this.getContentPane().add(jTextField1, BorderLayout.NORTH);
    this.getContentPane().add(jTextField2, BorderLayout.WEST);
    this.getContentPane().add(jTextField3, BorderLayout.SOUTH);
    this.getContentPane().add(jTextField4, BorderLayout.EAST);
    this.getContentPane().add(jTextField5, BorderLayout.CENTER);

    jTextField1.setNextFocusableComponent(jTextField5);
    jTextField2.setNextFocusableComponent(jTextField1);
    jTextField3.setNextFocusableComponent(jTextField2);
    jTextField4.setNextFocusableComponent(jTextField3);
    jTextField5.setNextFocusableComponent(jTextField4);

    (new FocusManager(jTextField5)).start();
  }

  //Overridden so we can exit on System Close
  protected void processWindowEvent(WindowEvent e)
  {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING)
    {
      System.exit(0);
    }
  }
}
class FocusManager extends Thread
{
  JComponent jc = null;
  public FocusManager(JComponent j)
  {
    super();
    jc = j;
  }
  public void run()
  {
    while (!jc.isVisible())
    {
      try
      {
        //wait to become visible
        sleep(100);
      }
      catch (Exception e)
      {

      }
    }
    while (!jc.hasFocus())
    {
      //use hte safest way to access swing resources from another thread
      SwingUtilities.invokeLater(new Runnable() { public void run() {jc.requestFocus();}});
      //wait to obtain focus
      try
      {
        sleep(100);
      }
      catch (Exception e)
      {

      }
    }
  }
}

Note the usage of another thread for setting the focus. I've found that the components should be visible in order to be able to set the focus. However, if I use the component shown event this does not always work, so I use a separate thread (it is harmless).

You can also see how the setNextFocusableComponent actually works.

This is only the frame object. You have to show it yourself.

Cheers,
  Nik
0
 
LVL 2

Expert Comment

by:mwibbels
ID: 2615429
As long as you're using a Frame, you can also override setVisible:

public void setVisible(boolean b) {
    super.setVisible(b);
    if (b) textField.requestFocus();
}
0
 

Author Comment

by:Priyan
ID: 2632703
hello Diakov,
Thanx a lot.It really helped me a lot
rgds
Priyan.T
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

747 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

11 Experts available now in Live!

Get 1:1 Help Now