Search code examples
javaswingjtextfield

Make a JTextField recieve input even with windows not focused


I'm working on a small personal project.

I have two JFrame windows, Viewer and Console.

  • Viewer only contains a subclass of JPanel, and it should respond to mouse input (clicking, mostly).
  • Console contains a button, a JTextField (the input), and a JTextArea (the output).

I need to make sure than when I press keys on my keyboard, the corresponding text appears in the Console JTextField, not only when the focus is held by the JTextField, but also when it's held any other component in my app.

In other words, I want the JTextField to accept input even right after I clicked on the Viewer frame.

Is this feasible?

In case that matters, I'm running win 8 x64 and I don't really care about portability (I'm the only one that will ever look at or use this code).

EDIT: here is an example of my code:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

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



public class main {
    public static Viewer v; 
    public static Console c;
    public static void main(String[] args) {
        v=new Viewer();
        c=new Console();
    }


    static class Viewer extends JFrame {
        public Viewer(){
            setSize(600,600);
            getContentPane().add(new myPanel());
            addMouseListener(new mouse());
            setVisible(true);

        }
    }
    static class myPanel extends JPanel{
        public void paintComponent(Graphics g){
            g.setColor(new Color((int)(Math.random()*255),(int)(Math.random()*255),(int)(Math.random()*255)));
            g.fillRect(0, 0, 600, 600);
        }
    }
    static class Console extends JFrame {
        public Console(){
            setSize(600,200);
            JTextField text=new JTextField();
            getContentPane().add(text);
            setVisible(true);

        }
    }
    static class mouse implements MouseListener{
        @Override
        public void mouseClicked(MouseEvent arg0) {
            v.repaint();
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
        }

        @Override
        public void mouseExited(MouseEvent arg0) {
        }

        @Override
        public void mousePressed(MouseEvent arg0) {
        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
        }
    }

}

In this example, after I click on the big window to change its color, if I want to write stuff in the other window I have to click on it first.


Solution

  • Can I suggest the KeyboardFocusManager? I've found this to be the easiest way to achieve what I believe you are trying to achieve

    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(
      new KeyEventDispatcher() {
         public void dispatchKeyEvent(KeyEvent ke) {
           //TODO: fill this in
         }
      });