title: how do i create a virtual keyboard that can be used to insert values in different jtextfields
body: <p>So I am trying to create a virtual keyboard that can insert values in a Jtextfield of another Jframe. The problem is that the data is overlapping when editing  other text fields. So, I tried renewing the object but it replaced the first Jtextfield value as well. what should i do with this, should i start from scratch or is there any other way? . Since, English is not my first language I am struggling to find the correct terminology to research the problem please enlighten me with your knowledge </p>
  
  <pre><code>import java.awt.*;     
  import javax.swing.*;  
  
  
  
  public class OnScreenKeyboard implements ActionListener {
  
  JFrame keyboard;
  static String keyboardKeys = "0123456789qwertyuiopasdfghjklzxcvbnm.&lt; ";
  JButton[] keys = new JButton[39];
  GridLayout gl;
  FlowLayout fl;
  Dimension buttondimension;
  JPanel panel1, panel2;
  
  JToggleButton capslock;
  private String message = "";
  
  public String getMessage() {
      return message;
  }
  
  public void setMessage(String message) {
      this.message = message;
  }
  
  public OnScreenKeyboard() {
      buttondimension = new Dimension(45, 40);
  
      fl = new FlowLayout();
  
      capslock = new JToggleButton("capslock");
  
      panel1 = new JPanel(fl);
      panel2 = new JPanel(fl);
  
      char[] key = keyboardKeys.toCharArray();
  
      for (int i = 0; i &lt; 39; i++) {
          keys[i] = new JButton(String.valueOf(key[i]));
          keys[i].setFont(new Font("Arial", Font.PLAIN, 13));
          if (i == 38) {
              keys[i].setPreferredSize(new Dimension(100, 30));
          } else {
  
              keys[i].setPreferredSize(buttondimension);
          }
          keys[i].addActionListener(this);
      }
  
      keyboard = new JFrame("Keyboard");
      keyboard.setSize(720, 220);
      keyboard.setLocationRelativeTo(null);
      keyboard.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      keyboard.setResizable(false);
  
      Container content = keyboard.getContentPane();
      content.setLayout(null);
      panel1.setBounds(1, 1, 500, 210);
      panel2.setBounds(510, 1, 200, 210);
  
      for (int i = 0; i &lt; 10; i++) {
          panel2.add(keys[i]);
      }
      for (int i = 10; i &lt; 39; i++) {
          panel1.add(keys[i]);
      }
  
      panel1.add(capslock);
  
      content.add(panel1);
      content.add(panel2);
  
      capslock.addActionListener(this);
  
      keyboard.setVisible(true);
  }
  
  public static void main(String[] args) {
      new OnScreenKeyboard();
  
  }
  
  public void reset(){
      message = "";
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
  
      for (int i = 0; i &lt; 36; i++) {
          if (e.getSource() == keys[i]) {
              setMessage(getMessage() + keys[i].getText());
              break;
          }
      }
  
      if (e.getSource() == capslock) {
          if (capslock.isSelected()) {
              for (int i = 10; i &lt; 36; i++) {
                  keys[i].setFont(new Font("Arial", Font.PLAIN, 12));
                  keys[i].setText(keys[i].getText().toUpperCase());
  
              }
          } else if (!capslock.isSelected()) {
              for (int i = 10; i &lt; 36; i++) {
                  keys[i].setFont(new Font("Arial", Font.PLAIN, 13));
                 keys[i].setText(keys[i].getText().toLowerCase());
  
              }
          }
      }
  
      setMessage(getMessage());
  
      //JOptionPane.showMessageDialog(null, getMessage());
  
  }
  
  }
  </code></pre>
  
  <p>this is the frame I am trying to put my values from the keyboard in</p>
  
  <pre><code>public class LoginScreen implements ActionListener, FocusListener {
  
  JFrame frame;
  Container content;
  FlowLayout fl;
  
  JTextField txtusername, txtpassword;
  JLabel lblusername, lblpassword;
  
  JPanel panel1, panel2;
  
  JButton keyboard, signup, signin;
  OnScreenKeyboard kyb;
  Dimension text;
  
  private void init() {
      text =new Dimension(100, 30);
  
      fl = new FlowLayout(FlowLayout.CENTER);
      lblusername = new JLabel("enter username");
      lblpassword = new JLabel("enter password");
      txtusername = new JTextField();
      txtpassword = new JPasswordField();
  
      keyboard = new JButton("keyboard");
      signup = new JButton("signup");
      signin = new JButton("sign in");
  
      panel1 = new JPanel(fl);
      panel2 = new JPanel(fl);
  
      keyboard = new JButton("keyboard");
  
      txtusername.setPreferredSize(text);
      txtpassword.setPreferredSize(text);
  
      kyb = new OnScreenKeyboard();
  
  }
  
  public LoginScreen() {
  
      init();
      frame = new JFrame("BorderLayoutDemo");
      frame.setTitle("Registration Form");
      frame.setSize(300, 300);
      frame.setDefaultCloseOperation(3);
  
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  
      content = frame.getContentPane();
  
      content.setLayout(new GridLayout(2, 1));
  
      panel1.add(lblusername);
      panel1.add(txtusername);
      panel1.add(lblpassword);
      panel1.add(txtpassword);
  
      panel2.add(signup);
      panel2.add(signin);
      panel2.add(keyboard);
  
      content.add(panel1);
      content.add(panel2);
      keyboard.addActionListener(this);
  
      txtusername.addFocusListener(this);
      txtpassword.addFocusListener(this);
  
  }
  
  public static void main(String[] args) {
      new LoginScreen();
  }
  
  @Override
  public void actionPerformed(ActionEvent e) {
  
      if (!kyb.keyboard.isVisible()) {
          if (e.getSource() == keyboard) {
              kyb = new OnScreenKeyboard();
          }
      }
  
  }
  
  
  @Override
  public void focusGained(FocusEvent e) {
     if(txtusername == e.getSource()){
         txtusername.setText(kyb.getMessage());
  
     }else if(txtpassword == e.getSource()){
  
         kyb.reset();
         txtpassword.setText(kyb.getMessage());
     }
  }
  
  @Override
  public void focusLost(FocusEvent e) {
  }
  </code></pre>