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