[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Focus Problem!! using setNextFocusableComponent

Posted on 2000-03-14
3
Medium Priority
?
479 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 200 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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