Hi,
I am trying to write code for a virtual keyboard which opens up when keyboard button is pressed and helps to enter the text in a textfield.
But i am getting following problems :
note: i have marked the problems in the code as comments.
CODE
public class Utilities extends JPanel
{
private JRadioButton radio[];
private ButtonGroup group;
private JButton keyboard;
private JLabel label;
private JComboBox comboBox;
private JTextField name;
JTextComponent targetComponent = null;
public Utilities() {
label = new JLabel("Login");
add(label);
name = new JTextField();
name.setColumns(20);
add(name);
targetComponent = name;
keyboard = new JButton("keyboard");
add(keyboard);
keyboard.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
new Keyboard(targetComponent);
}
});
}
// KEYBOARD INNER CLASS
// ********************************************************************************
*************************************************
public class Keyboard extends JFrame implements FocusListener{
String keyName[] = {"1","2","3","4","5","6","7","8","9","0","-","=",
"Q","W","E","R","T","Y","U","I","O","P","[","]",
"A","S","D","F","G","H","J","K","L",";","'",
"Z","X","C","V","B","N","M",",",".","/"};
String[] specialCharacters = {"@","#","$","%","^","&","*","(",")"};
char[] specialCharactersKey = {'@','#','$','%','^','&','*','(',')'};
JComboBox select;
char[] key = {'1','2','3','4','5','6','7','8','9','0','-','=',
'Q','W','E','R','T','Y','U','I','O','P','[',']',
'A','S','D','F','G','H','J','K','L',';','\'',
'Z','X','C','V','B','N','M',',','.','/'
};
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_Q,
KeyEvent.VK_W,
KeyEvent.VK_E,
KeyEvent.VK_R,
KeyEvent.VK_T,
KeyEvent.VK_Y,
KeyEvent.VK_U,
KeyEvent.VK_I,
KeyEvent.VK_O,
KeyEvent.VK_P,
KeyEvent.VK_OPEN_BRACKET,
KeyEvent.VK_CLOSE_BRACKET,
KeyEvent.VK_A,
KeyEvent.VK_S,
KeyEvent.VK_D,
KeyEvent.VK_F,
KeyEvent.VK_G,
KeyEvent.VK_H,
KeyEvent.VK_J,
KeyEvent.VK_K,
KeyEvent.VK_L,
KeyEvent.VK_SEMICOLON,
KeyEvent.VK_SEMICOLON, // *PROBLEM 1 : not able to find apostophe : '
KeyEvent.VK_Z,
KeyEvent.VK_X,
KeyEvent.VK_C,
KeyEvent.VK_V,
KeyEvent.VK_B,
KeyEvent.VK_N,
KeyEvent.VK_M,
KeyEvent.VK_COMMA,
KeyEvent.VK_PERIOD,
KeyEvent.VK_SLASH,
};
JButton button[] = new JButton[keyName.length];
JButton backButton;
JButton tabButton;
JButton enterButton;
JButton SpaceButton;
JButton capsButton;
JButton shiftButton;
JButton deleteButton;
JButton spaceButton;
JPanel panel;
JTextComponent target = null;
public int i = 0;
// ********************************************************************************
********************************************************************
public Keyboard(JTextComponent 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;
// SPECIAL CHARACTER SELECT BOX
select = new JComboBox(specialCharacters);
panel.add(select,c);
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++;
}
// BACKSPACE BUTTON
backButton = new JButton("<<--");
panel.add(backButton,c);
c.gridy = 1;
c.gridx =0;
// TAB BUTTON
tabButton = new JButton("TAB");
panel.add(tabButton,c);
c.gridx =1;
// BUTTONS Q TO ]
for(int k=12; k<=23; k++)
{
button[k] = new JButton(keyName[k]);
panel.add(button[k], c);
c.gridx++;
}
// DELETE BUTTON
deleteButton = new JButton("Del");
panel.add(deleteButton,c);
c.gridx =0;
c.gridy = 2;
//CAPS BUTTON
capsButton = new JButton("Caps");
panel.add(capsButton,c);
c.gridx =1;
// BUTTONS A TO '
for(int k=24; k<=34; k++)
{
button[k] = new JButton(keyName[k]);
panel.add(button[k], c);
c.gridx++;
}
// ENTER BUTTON
enterButton = new JButton("Enter");
panel.add(enterButton,c);
c.gridy = 3;
c.gridx =0;
// SHIFT BUTTON
shiftButton = new JButton("Shift");
panel.add(shiftButton,c);
c.gridx =1;
// BUTTONS Z TO /
for(int k=35; k < keyName.length; k++)
{
button[k] = new JButton(keyName[k]);
panel.add(button[k], c);
c.gridx++;
}
c.gridy = 4;
c.gridx = 0;
//SPACE BUTTON
spaceButton = new JButton("S p a c e");
panel.add(spaceButton,c);
// ACTION LISTENERS & #58;############################################################################
########################
// SELECT LISTENER
select.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent ie)
{
//
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_TYPED,System.currentTimeMillis(), 0,KeyEvent.VK_UNDEFINED
,specialCharactersKey[select.getSelectedIndex()]));
}
}); // * PROBLEM 2 : Getting 2 same characters instead of one while //making item selection
// BUTTON ARRAY LISTENER
for( i =0; i<keyName.length; i++)
{
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,keyEvent,KeyEvent.CHAR_UNDEFINED));
}});
} // PROBLEM 3 : Getting array out of bound exception. But still could not //debug
// TAB LISTEN
tabButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,KeyEvent.VK_TAB,KeyEvent.CHAR_UNDEFINED));
}
});
//DELETE LISTEN
deleteButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,KeyEvent.VK_DELETE,KeyEvent.CHAR_UNDEFINED));
// target.requestFocus();
}
});
//BACKSPACE LISTEN
backButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,KeyEvent.VK_BACK_SPACE,KeyEvent.CHAR_UNDEFINED));
}
});
//SPACE LISTEN
spaceButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,KeyEvent.VK_SPACE,KeyEvent.CHAR_UNDEFINED));
}
});
//SHIFT LISTEN
shiftButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,KeyEvent.VK_SHIFT,KeyEvent.CHAR_UNDEFINED));
}
});
//CAPS LISTEN
capsButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
target.dispatchEvent(new KeyEvent(target,KeyEvent.KEY_PRESSED,System.currentTimeMillis(), 0,KeyEvent.VK_CAPS_LOCK,KeyEvent.CHAR_UNDEFINED));
}
});
}
// ********************************************************************************
***************************************************************
public void focusGained(FocusEvent e)
{
}
public void focusLost(FocusEvent e)
{
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("utilities");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Utilities newContentPane = new Utilities();
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) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
If you find any other problems with the code or way to make the code short please tell me. I will be very thankful to you.>
This post has been edited by alcdotcom: 3 Jul, 2007 - 01:52 AM