Super26
asked on
Giving a JButton the focus
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.
Did you get your table focus issue fixed?
ASKER
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.
Have u try what I suggested yet?
(It is not a bug).
(It is not a bug).
ASKER
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.
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/
Perhaps you should spend some time improving your Java skills. Sun has a good overall tutorial at:
http://java.sun.com/docs/books/tutorial/
requestFocus() is the correct method.
Can you post your code?
Can you post your code?
Combine requestFocus() with setFocusPainted(true) - I believe that's the method name.
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(ActionEven t ae) {
b2.requestFocus();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven t ae) {
b3.requestFocus();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven t ae) {
b1.requestFocus();
}
});
}
public static void main(String[] args) {
test t = new test();
t.setSize(300, 300);
t.setDefaultCloseOperation (JFrame.EX IT_ON_CLOS E);
t.setVisible(true);
}
}
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
getContentPane().add(b1);
getContentPane().add(b2);
getContentPane().add(b3);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
b2.requestFocus();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
b3.requestFocus();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
b1.requestFocus();
}
});
}
public static void main(String[] args) {
test t = new test();
t.setSize(300, 300);
t.setDefaultCloseOperation
t.setVisible(true);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
thx a lot that help me :)