Giving a JButton the focus

Super26
Super26 used Ask the Experts™
on
I am having a real problem with setting the focus on a JButton.  I have a frame that holds a few panels, and one of them holds a few radio buttons and another has a couple of buttons for the user to click.  Currently the focus is on the first radio button, what I want is for my OK button to get the focus.  I tried buttonOK.requestFocus() and everything, but that doesn't seem to work.  I am hoping someone could help me.  Thanks.

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mick BarryJava Developer
Top Expert 2010

Commented:
Did you get your table focus issue fixed?

Author

Commented:
Thanks for asking.  No, I have practically lost hope on that.  I am still searching for other ways to get rid of that bug.  I put that table in a panel, maybe if I can get it to just not tab over the panel, that would even work.  
Mick BarryJava Developer
Top Expert 2010

Commented:
Have u try what I suggested yet?

(It is not a bug).
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Author

Commented:
I am afraid I might need a thorough example.  I tried with what you gave me but I kept on getting errors.  It is really confusing me.  
Mick BarryJava Developer
Top Expert 2010

Commented:
The example I gave you was pretty complete, all you needed to add was your required constructor.

Perhaps you should spend some time improving your Java skills. Sun has a good overall tutorial at:
http://java.sun.com/docs/books/tutorial/
Mick BarryJava Developer
Top Expert 2010

Commented:
requestFocus() is the correct method.
Can you post your code?
Ovi

Commented:
Combine requestFocus() with setFocusPainted(true) - I believe that's the method name.
Ovi

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

public class test extends JFrame {
  JButton b1 = new JButton("B1");
  JButton b2 = new JButton("B2");
  JButton b3 = new JButton("B3");

  public test() {
    getContentPane().setLayout(new GridLayout());
    getContentPane().add(b1);
    getContentPane().add(b2);
    getContentPane().add(b3);
   
    b1.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        b2.requestFocus();
      }
    });
    b2.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        b3.requestFocus();
      }
    });
    b3.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        b1.requestFocus();
      }
    });
   
  }

  public static void main(String[] args) {
    test t = new test();
    t.setSize(300, 300);
    t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    t.setVisible(true);
  }
}
Commented:
You need to set the focus to the button AFTER the frame is shown. Put this code in the frame's constructor:

addWindowListener(
  new WindowAdapter() {
    public void windowOpened(WindowEvent windowEvent) {
      buttonOK.requestFocus();
    }
  }
);

Author

Commented:
Thanks all of you guys.  I just didn't put request focus in the right place.  I wish I could give all of you points.  Thanks a lot.

Commented:
thx a lot that help me :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial