Solved

Focus Problem!! using setNextFocusableComponent

Posted on 2000-03-14
3
405 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

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
groupSum6 challenge 6 77
simple java question 3 44
JavaScript/Java - Changing an image background color 4 56
eclipse shortcuts 9 45
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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 covers a step-by-step guide to install VisualVM launcher in eclipse.

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

23 Experts available now in Live!

Get 1:1 Help Now