0

I want to be able to type in textfield when my frame brought foreground. Program is set to bring my Frame to the foreground after 5 seconds and set focus on textfield. I start the program then click another window. My frame shows up after 5 seconds with cursor blinking. But when i type something it doesnt actually taken as input by textfield. I also implemented FocusListener to confirm that focus set to textfield when frame brought to foreground. My operating system is Windows 10.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TestMain implements FocusListener {
    /**
     * Create the GUI and show it. For thread safety, this method should be
     * invoked from the event-dispatching thread.
     */
    public JFrame frame = null;

    private void createAndShowGUI() {
        // Create and set up the window.
        frame = new JFrame();
        frame.addFocusListener(this);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        JLabel label1 = new JLabel("Label");
        panel.add(label1);

        JTextField textField1 = new JTextField();
        textField1.setPreferredSize(new Dimension(300, 40));
        panel.add(textField1);
        textField1.addFocusListener(this);
        label1.addFocusListener(this);

        frame.addWindowListener(new WindowAdapter() {
            // Program Closing Alert
            public void windowActivated(WindowEvent e) {
                System.out.println("window activated");
                textField1.requestFocusInWindow();
            }
        });

        frame.getContentPane().add(panel, BorderLayout.CENTER);

        // Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        TestMain mn = null;
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        mn = new TestMain();
        mn.createAndShowGUI();

        System.out.println("START");
        try {
            Thread.sleep(5000);
            mn.frame.setAlwaysOnTop(true);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void focusGained(FocusEvent e) {
        System.out.println("Focus Gained by " + e.getComponent().getClass().getName());

    }

    @Override
    public void focusLost(FocusEvent e) {
        System.out.println("Focus Lost by " + e.getComponent().getClass().getName());

    }

}

UPDATE : When i add

frame.setLocationByPlatform( true );

after

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

it works only for the first time. if i change main function as

try {
    Thread.sleep(5000);
    mn.frame.setAlwaysOnTop(true);
    mn.frame.setAlwaysOnTop(false);
    Thread.sleep(5000);
    mn.frame.setAlwaysOnTop(true);
    mn.frame.setAlwaysOnTop(false);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

it doesnt work for the second time

Ali Atıl
  • 121
  • 7

2 Answers2

0

You do bring focus to the JTextField but you dont b ring focus to the frame itself. Bringing the frame to the front and bringing the focus to the frame is different. A solution similar can be found here: How to set focus the already running application?

Community
  • 1
  • 1
0

Moving your frame doesn't focus your textfield. You should add toFront() after set always on top so if the Window is visible, brings the Window to the front and may make it the focused Window.

try {
    Thread.sleep(5000);
    mn.frame.setVisible ( true );
    mn.frame.toFront ( );

} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Rcordoval
  • 1,932
  • 2
  • 19
  • 25
  • i've tried this but problem still remains. i've noticed that after adding "mn.frame.tofront()", when it's brought to foreground it keeps blinking on taskbar. – Ali Atıl Dec 28 '16 at 20:46
  • @Splash What about now? check my edited answer (i'm on linux and it's working, i need your confirmation) – Rcordoval Dec 28 '16 at 20:49
  • when i change "setAlwaysOnTop(true);" with "setVisible(true);" it also doesnt work. i've put "frame.setLocationByPlatform( true );" statement after "frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);" and it works now but i dont know if that has a side effect. – Ali Atıl Dec 28 '16 at 20:55
  • What version of the JRE are you using. In 1.7 there is a setAutoRequestFocus on the window that should automatically receive focus on toFront() – Jayfray Dec 28 '16 at 22:08