Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Focus Problem!! using setNextFocusableComponent

Posted on 2000-03-14
3
Medium Priority
?
468 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
[X]
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
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

618 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