Solved

Focus Problem!! using setNextFocusableComponent

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
use lov values 2 72
jboss wildfly 10.1 10 420
Chrome and Firefox Java 5 67
web project error add remove 1 51
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

756 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