Dear Members,
I developed following class to creat a virtual key boad. While user will click it should type that number to login field.
The class is compiled with jdk1.6.0_14. successfully
But while button is clicked its not typing that number to login filed.
Kindly help to resolve this problem
Thanks & Regards,

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;


public class Util extends JPanel
{

    private JButton keyboard;
    private JLabel label;
    private JTextField name;

    JTextComponent targetComponent = null;

    public Util() {

    label = new JLabel("Login");
    add(label);
    name = new JTextField();
    name.setColumns(20);
    add(name);
    keyboard = new JButton("keyboard");
    add(keyboard);
    keyboard.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
        new Keyboard(name);
    }
    });
    }

    // KEYBOARD INNER CLASS
    // ////********************************************************************************
    //*************************************************

    public class Keyboard extends JFrame implements FocusListener{
    String keyName[] = {"1","2","3","4","5","6","7","8","9","0","-","="};


    int keyEvent[] ={KeyEvent.VK_1,
    KeyEvent.VK_2,
    KeyEvent.VK_3,
    KeyEvent.VK_4,
    KeyEvent.VK_5,
    KeyEvent.VK_6,
    KeyEvent.VK_7,
    KeyEvent.VK_8,
    KeyEvent.VK_9,
    KeyEvent.VK_0,
    KeyEvent.VK_MINUS,
    KeyEvent.VK_EQUALS,
    KeyEvent.VK_SLASH,
    };


    JButton button[] = new JButton[keyName.length];


    JPanel panel;
    JTextField target = null;
    public int i = 0;
    // ////********************************************************************************
    //********************************************************************

    public Keyboard(JTextField target)
    {

    this.target = target;
    addComponentsToPane();

    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


    this.setPreferredSize(new Dimension(550,150));
    this.setLocation(600,100);
    this.setContentPane(panel);
    this.pack();
    this.setVisible(true);

    }
    // ////********************************************************************************
    ////*********************************************************************

    public void addComponentsToPane()
    {


    panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx =0;
    c.gridy = 0;
    c.gridx = 1;
    // BUTTONS 1 TO =
    for(int k = 0; k <= 11; k++)
    {

    button[k] = new JButton(keyName[k]);
    button[k].setPreferredSize(new Dimension(400,400));
    panel.add(button[k], c);

    c.gridx++;

    }

    // BUTTON ARRAY LISTENER
    for( i =0; i<keyName.length; i++)
    {
        button[i].addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,keyEvent[i],KeyEvent.CHAR_UNDEFINED));
        }});
    } 

    }

    // ////********************************************************************************
    //***************************************************************

    public void focusGained(FocusEvent e)
    {

    }

    public void focusLost(FocusEvent e)
    {
    }




    }
    private static void createAndShowGUI() {
    JFrame frame = new JFrame("Util");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Util newContentPane = new Util();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);
    frame.setPreferredSize(new Dimension(650, 800));
    frame.setLocation(350,30);

    //Main main = new Main();

    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String[] args) {

    createAndShowGUI();

    }
}

1. add KeyListener to target

target.addKeyListener(new KeyListener() {...

or
2. use direct method

// BUTTON ARRAY LISTENER
        for (i = 0; i < keyName.length; i++) {
            button[i].addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    //j3c
                    String s = target.getText() + e.getActionCommand();
                    target.setText(s);
                    //target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, keyEvent[i], KeyEvent.CHAR_UNDEFINED));
                }
            });
        }

    }

Thanks a lot quuba

1. add KeyListener to target

target.addKeyListener(new KeyListener() {...

or
2. use direct method

// BUTTON ARRAY LISTENER
        for (i = 0; i < keyName.length; i++) {
            button[i].addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    //j3c
                    String s = target.getText() + e.getActionCommand();
                    target.setText(s);
                    //target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, keyEvent[i], KeyEvent.CHAR_UNDEFINED));
                }
            });
        }

    }

Is another one resolution. Just using dispatchEvent.
target not need additional listeners.
Example for button[0] and button[1]:

button[0].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, keyName[0].charAt(0)));
            }
        });
        button[1].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, keyName[1].charAt(0)));
            }
        });
        //....

or shorter version

for (int k = 0; k < keyName.length; k++) {

            button[k] = new JButton(keyName[k]);
            button[k].addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, /* TODO: WANTED char  from e-event     */));
                }
            });
            button[k].setPreferredSize(new Dimension(400, 400));
            panel.add(button[k], c);

            c.gridx++;

        }

Thanks a lot quuba

Dear Sir,
i have attached the whole code with class file for your consideration.
Infact this code was written in jdk1.2.2 and its working well there
but while migrated to java se 6 its not giving desired results
but it successfully compiled with java se 6.It has java se 6 compile classes in myproj.zip (attachment)
Action desired: While user click to virtual key it should type that letter to focused field but its not doing in java se 6.
Kindly help to resolve this problem

Thanks

Infact this code was written in jdk1.2.2 and its working well there

In release 1.4, the focus subsystem was rearchitected.

Use the piece of code. This adds an AWTEventListener to receive all AWTEvents dispatched system-wide that conform to the given eventMask.

public GUILayer() {        
        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

            public void eventDispatched(AWTEvent event) {
                System.out.println("AWTEventListener:eventDispatched " + event);
            }
        }, -1);
//...

VirtualKeyButton: change the source/target respectively to Component: usernameTextField.getTextField() and sfuIdTextField.getTextField(). Make same changes in constructor.

//...
 // Otherwise add a mouse motion listener that dispatches
        // a key typed event that matches the character the key
        // represents
        else {
            // create an anonymous class for handling the mousePressed events
            addMouseListener(new MouseAdapter() {

                public void mousePressed(MouseEvent evt) {
                    if (keyEventManager != null) {

                        keyEventManager.keyTyped(
                                new KeyEvent(target, //VirtualKeyButton.this
                                KeyEvent.KEY_TYPED,
                                System.currentTimeMillis(),
                                0,
                                KeyEvent.VK_UNDEFINED,
                                theKey));
                    }
                }
            });
        }
    }

At and realize switching through buttons with ImageIcon.


http://www.javaworld.com/javaworld/jw-08-2007/jw-08-swingthreading.html?page=1

http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html

http://tech.stolsvik.com/2009/03/awt-swing-event-pumping-and-targeting.html
Dear Sir.

commented: Nice explanation. +11

Thanks a lot
i used java.awt.Robot Class to resolve this and its working well
But i will do modify my code as u adviced and will test it and will inform you as well the latest progress
Thanks once again
msalahu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.