下面列出了javax.swing.JButton#requestFocus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public SingleEntryDialog(Component parent, String entryLabel,
String buttonLabel, ActionListener buttonListener)
{
super();
entry = new JTextField(entryLabel);
entry.setForeground(Color.WHITE);
entry.setBackground(Color.DARK_GRAY);
entry.setBorder(new LineBorder(Color.GRAY));
entry.setCaretColor(Color.WHITE);
entry.setFont(new Font("Tahoma", Font.PLAIN, 22));
entry.addFocusListener(new TextFieldFocusListener(entry));
contentPanel.add(entry, "cell 0 1, grow, gapx 2 2");
JButton btnConfirm = new ModernButton(buttonLabel);
btnConfirm.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnConfirm.addActionListener(buttonListener);
contentPanel.add(btnConfirm, "cell 0 3, alignx center");
JLabel spacer = new JLabel(" ");
contentPanel.add(spacer, "cell 0 4");
finalize(parent);
btnConfirm.requestFocus();
}
public DoubleEntryDialog(Component parent, String entry1Label,
String entry2Label, String buttonLabel, ActionListener buttonListener)
{
super();
entry1 = new ModernTextField(entry1Label);
entry1.addFocusListener(new TextFieldFocusListener(entry1));
contentPanel.add(entry1, "cell 0 1, grow, gapx 2 2");
entry2 = new ModernTextField(entry2Label);
entry2.addFocusListener(new TextFieldFocusListener(entry2));
contentPanel.add(entry2, "cell 0 2, grow, gapx 2 2");
JButton btnConfirm = new ModernButton(buttonLabel);
btnConfirm.setFont(new Font("Tahoma", Font.PLAIN, 18));
btnConfirm.addActionListener(buttonListener);
contentPanel.add(btnConfirm, "cell 0 4, alignx center");
JLabel spacer = new JLabel(" ");
contentPanel.add(spacer, "cell 0 5");
finalize(parent);
btnConfirm.requestFocus();
}
public void handleWindow(Window window, int eventID) {
if (! SwingUtils.clickButton(window, "OK")) {
Utils.logError("The system is not currently available.");
return;
}
if (LoginManager.loginManager().getLoginFrame() != null) {
JButton button2 =
SwingUtils.findButton(LoginManager.loginManager().getLoginFrame(), "Login");
button2.requestFocus();
KeyEvent ke =
new KeyEvent(button2, KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
KeyEvent.ALT_DOWN_MASK,
KeyEvent.VK_F4,
KeyEvent.CHAR_UNDEFINED);
button2.dispatchEvent(ke);
}
}
public void handleWindow(Window window, int eventID) {
if (! SwingUtils.clickButton(window, "OK")) {
Utils.logError("The system is not currently available.");
return;
}
if (LoginManager.loginManager().getLoginFrame() != null) {
JButton button2 =
SwingUtils.findButton(LoginManager.loginManager().getLoginFrame(), "Login");
button2.requestFocus();
KeyEvent ke =
new KeyEvent(button2, KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
KeyEvent.ALT_DOWN_MASK,
KeyEvent.VK_F4,
KeyEvent.CHAR_UNDEFINED);
button2.dispatchEvent(ke);
}
}
/**
*
* @Title: init
* @Description: Component Initialization
* @param
* @return void
* @throws
*/
private void init()
{
this.setTitle(RESTConst.ABOUT_TOOL);
this.setLayout(new BorderLayout(RESTConst.BORDER_WIDTH, RESTConst.BORDER_WIDTH));
JPanel pnlDialog = new JPanel();
pnlDialog.setLayout(new BorderLayout());
JLabel lblTitle = new JLabel("<html><h3>" + RESTConst.REST_CLIENT_VERSION + "</h3></html>");
JPanel pnlNorth = new JPanel();
pnlNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
pnlNorth.add(lblTitle);
pnlDialog.add(pnlNorth, BorderLayout.NORTH);
JPanel pnlCenter = new JPanel();
pnlCenter.setLayout(new GridLayout(1, 1));
JTextPane tp = new JTextPane();
tp.setEditable(false);
tp.setContentType("text/html");
tp.setText(UIUtil.contents(RESTConst.WISDOM_TOOL_ORG));
pnlCenter.add(new JScrollPane(tp));
pnlDialog.add(pnlCenter, BorderLayout.CENTER);
JPanel pnlSouth = new JPanel();
pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton btnOK = new JButton(RESTConst.OK);
btnOK.addActionListener(this);
btnOK.requestFocus();
getRootPane().setDefaultButton(btnOK);
pnlSouth.add(btnOK);
pnlDialog.add(pnlSouth, BorderLayout.SOUTH);
this.setContentPane(pnlDialog);
this.setIconImage(UIUtil.getImage(RESTConst.LOGO));
this.pack();
}
public MiniReportDisplay(JFrame parent, Client client) {
super(parent, Messages.getString("MiniReportDisplay.title"), true); //$NON-NLS-1$
butOkay = new JButton(Messages.getString("Okay")); //$NON-NLS-1$
butOkay.addActionListener(this);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(BorderLayout.SOUTH, butOkay);
setupReportTabs(client);
setSize(GUIPreferences.getInstance().getMiniReportSizeWidth(),
GUIPreferences.getInstance().getMiniReportSizeHeight());
doLayout();
setLocation(GUIPreferences.getInstance().getMiniReportPosX(),
GUIPreferences.getInstance().getMiniReportPosY());
// closing the window is the same as hitting butOkay
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
actionPerformed(new ActionEvent(butOkay,
ActionEvent.ACTION_PERFORMED, butOkay.getText()));
}
});
butOkay.requestFocus();
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
/**
*
* @Title: init
* @Description: Component Initialization
* @param
* @return void
* @throws
*/
private void init()
{
this.setTitle(RESTConst.DONATE_BY_PAY);
this.setLayout(new BorderLayout(RESTConst.BORDER_WIDTH, RESTConst.BORDER_WIDTH));
JPanel pnlDialog = new JPanel();
pnlDialog.setLayout(new BorderLayout());
JLabel lblDonate = new JLabel();
lblDonate.setIcon(UIUtil.getIcon(RESTConst.DONATE_ICON));
lblDonate.setToolTipText(RESTConst.DONATE_BY_PAY);
JPanel pnlNorth = new JPanel();
pnlNorth.setLayout(new FlowLayout(FlowLayout.CENTER));
pnlNorth.add(lblDonate);
pnlDialog.add(pnlNorth, BorderLayout.NORTH);
JPanel pnlCenter = new JPanel();
pnlCenter.setLayout(new GridLayout(1, 1));
JTextPane tp = new JTextPane();
tp.setEditable(false);
tp.setContentType("text/html");
tp.setText(UIUtil.contents(RESTConst.DONATION));
pnlCenter.add(new JScrollPane(tp));
pnlDialog.add(pnlCenter, BorderLayout.CENTER);
JPanel pnlSouth = new JPanel();
pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton btnOK = new JButton(RESTConst.OK);
btnOK.addActionListener(this);
btnOK.requestFocus();
getRootPane().setDefaultButton(btnOK);
pnlSouth.add(btnOK);
pnlDialog.add(pnlSouth, BorderLayout.SOUTH);
this.setContentPane(pnlDialog);
this.setIconImage(UIUtil.getImage(RESTConst.LOGO));
this.pack();
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
private static void createAndShowGUI() {
mainFrame = new JFrame("Bug 8033699 - 8 Tests for Grouped/Non Group Radio Buttons");
btnStart = new JButton("Start");
btnEnd = new JButton("End");
btnMiddle = new JButton("Middle");
JPanel box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
box.setBorder(BorderFactory.createTitledBorder("Grouped Radio Buttons"));
radioBtn1 = new JRadioButton("A");
radioBtn2 = new JRadioButton("B");
radioBtn3 = new JRadioButton("C");
ButtonGroup btnGrp = new ButtonGroup();
btnGrp.add(radioBtn1);
btnGrp.add(radioBtn2);
btnGrp.add(radioBtn3);
radioBtn1.setSelected(true);
box.add(radioBtn1);
box.add(radioBtn2);
box.add(btnMiddle);
box.add(radioBtn3);
radioBtnSingle = new JRadioButton("Not Grouped");
radioBtnSingle.setSelected(true);
mainFrame.getContentPane().add(btnStart);
mainFrame.getContentPane().add(box);
mainFrame.getContentPane().add(radioBtnSingle);
mainFrame.getContentPane().add(btnEnd);
mainFrame.getRootPane().setDefaultButton(btnStart);
btnStart.requestFocus();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.Y_AXIS));
mainFrame.setSize(300, 300);
mainFrame.setLocation(200, 200);
mainFrame.setVisible(true);
mainFrame.toFront();
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}
@Override
public void start() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(AWTEvent e) {
System.err.println(e);
}
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
boolean gained = false;
final Robot robot = Util.createRobot();
JFrame frame1 = new JFrame("Main Frame");
final JButton b1 = new JButton("button1");
frame1.add(b1);
frame1.pack();
frame1.setLocation(0, 300);
Util.showWindowWait(frame1);
final JFrame frame2 = new JFrame("Test Frame");
final JButton b2 = new JButton("button2");
frame2.add(b2);
frame2.pack();
frame2.setLocation(300, 300);
b2.setEnabled(false);
b2.requestFocus();
Util.showWindowWait(frame2);
robot.delay(500);
//
// It's expeced that the focus is restored to <button1>.
// If not, click <button1> to set focus on it.
//
if (!b1.hasFocus()) {
gained = Util.trackFocusGained(b1, new Runnable() {
public void run() {
Util.clickOnComp(b1, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Unexpected state: focus is not on <button1>");
}
}
robot.delay(500);
//
// Click <button2>, check that focus is set on the parent frame.
//
gained = false;
gained = Util.trackFocusGained(frame2, new Runnable() {
public void run() {
Util.clickOnComp(b2, robot);
}
}, 5000, false);
if (!gained) {
throw new RuntimeException("Test failed: focus wasn't set to <frame2>");
}
System.out.println("Test passed.");
}