Riccardo Vailati
Riccardo Vailati

Reputation: 203

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.

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.

Upvotes: 1

Views: 104

Answers (2)

Jeff Demanche
Jeff Demanche

Reputation: 764

One inelegant solution is to create a KeyListener, which feeds typed characters to your console, although how exactly this happens depends on how you create your components. For the sake of this example I'll pretend to do it through a static method in your Console class (preferably you'd have access to an instance of Console):

public class ApplicationKeyListener implements KeyListener {
    public ApplicationKeyListener() {
    }

    // Other necessary methods...

    public void keyPressed(KeyEvent e) {
        char pressed = e.getKeyChar();
        Console.feedChar(pressed);
    }
}

And then in Console, make sure your JTextField object is global and static, then set up the feedChar() method.

public class Console extends JFrame {
    private static JTextField consoleTextField;

    public Console() {
        consoleTextField = new JTextField();
        // ...
    }

    public static feedChar(char c) {
        consoleTextField.setText(consoleTextField.getText() + c.toString());
    }
}

Then finally you'd have to add this listener to all JFrames and their children.

public class Viewer extends JFrame {
    public Viewer() {
        ApplicationKeyListener kl = new ApplicationKeyListener();
        addKeyListener(kl);
        for (Component child : this.getComponents()) {
            child.addKeyListener(kl);
        }
    }
}

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35106

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
     }
  });

Upvotes: 1

Related Questions