Java Code Examples for javax.swing.JButton.requestFocus()

The following are Jave code examples for showing how to use requestFocus() of the javax.swing.JButton class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Equella   File: LoginDialog.java   Source Code and License Vote up 4 votes
private void setup(String server)
{
	okPressed = false;

	JLabel usernameLabel = new JLabel("Username");
	JLabel passwordLabel = new JLabel("Password");

	ok = new JButton("OK");
	cancel = new JButton("Cancel");
	username = new JTextField();
	password = new JPasswordField();

	ok.addActionListener(this);
	cancel.addActionListener(this);

	final int height1 = username.getPreferredSize().height;
	final int height2 = ok.getPreferredSize().height;
	final int width1 = Math.max(usernameLabel.getPreferredSize().width, passwordLabel.getPreferredSize().width);
	final int width2 = cancel.getPreferredSize().width;

	final int[] rows = {height1, height1, TableLayout.FILL, height2,};
	final int[] cols = {width1, TableLayout.FILL, width2, width2,};

	JPanel all = new JPanel(new TableLayout(rows, cols, 5, 5));
	all.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

	all.add(usernameLabel, new Rectangle(0, 0, 1, 1));
	all.add(username, new Rectangle(1, 0, 3, 1));
	all.add(passwordLabel, new Rectangle(0, 1, 1, 1));
	all.add(password, new Rectangle(1, 1, 3, 1));
	all.add(ok, new Rectangle(2, 3, 1, 1));
	all.add(cancel, new Rectangle(3, 3, 1, 1));

	getContentPane().add(all);

	ok.requestFocus();

	setSize(310, 150);
	setTitle("Login to " + server);
	getRootPane().setDefaultButton(ok);
	ComponentHelper.centreOnScreen(this);
}
 
Example 2
Project: jdk8u-jdk   File: ResetMostRecentFocusOwnerTest.java   Source Code and License Vote up 4 votes
@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.");
}
 
Example 3
Project: openjdk-jdk10   File: ResetMostRecentFocusOwnerTest.java   Source Code and License Vote up 4 votes
@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.");
}
 
Example 4
Project: openjdk-jdk10   File: bug8033699.java   Source Code and License Vote up 4 votes
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();
}