Virtual key is not working java 6

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 7
Reputation: msalahu is an unknown quantity at this point 
Solved Threads: 0
msalahu msalahu is offline Offline
Newbie Poster

Virtual key is not working java 6

 
0
  #1
Aug 2nd, 2009
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();

}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Virtual key is not working java 6

 
0
  #2
Aug 2nd, 2009
1. add KeyListener to target
  1. target.addKeyListener(new KeyListener() {...
or
2. use direct method
  1. // BUTTON ARRAY LISTENER
  2. for (i = 0; i < keyName.length; i++) {
  3. button[i].addActionListener(new ActionListener() {
  4.  
  5. public void actionPerformed(ActionEvent e) {
  6. //j3c
  7. String s = target.getText() + e.getActionCommand();
  8. target.setText(s);
  9. //target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, keyEvent[i], KeyEvent.CHAR_UNDEFINED));
  10. }
  11. });
  12. }
  13.  
  14. }
Last edited by quuba; Aug 2nd, 2009 at 1:46 pm. Reason: added line:target.setText(s);
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: msalahu is an unknown quantity at this point 
Solved Threads: 0
msalahu msalahu is offline Offline
Newbie Poster

Re: Virtual key is not working java 6

 
0
  #3
Aug 2nd, 2009
Thanks a lot quuba
Originally Posted by quuba View Post
1. add KeyListener to target
  1. target.addKeyListener(new KeyListener() {...
or
2. use direct method
  1. // BUTTON ARRAY LISTENER
  2. for (i = 0; i < keyName.length; i++) {
  3. button[i].addActionListener(new ActionListener() {
  4.  
  5. public void actionPerformed(ActionEvent e) {
  6. //j3c
  7. String s = target.getText() + e.getActionCommand();
  8. target.setText(s);
  9. //target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, keyEvent[i], KeyEvent.CHAR_UNDEFINED));
  10. }
  11. });
  12. }
  13.  
  14. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Virtual key is not working java 6

 
0
  #4
Aug 2nd, 2009
Is another one resolution. Just using dispatchEvent.
target not need additional listeners.
Example for button[0] and button[1]:
  1. button[0].addActionListener(new ActionListener() {
  2. public void actionPerformed(ActionEvent e) {
  3. target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, keyName[0].charAt(0)));
  4. }
  5. });
  6. button[1].addActionListener(new ActionListener() {
  7. public void actionPerformed(ActionEvent e) {
  8. target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, keyName[1].charAt(0)));
  9. }
  10. });
  11. //....

or shorter version
  1. for (int k = 0; k < keyName.length; k++) {
  2.  
  3. button[k] = new JButton(keyName[k]);
  4. button[k].addActionListener(new ActionListener() {
  5.  
  6. public void actionPerformed(ActionEvent e) {
  7. target.dispatchEvent(new KeyEvent(target, KeyEvent.KEY_TYPED, System.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, /* TODO: WANTED char from e-event */));
  8. }
  9. });
  10. button[k].setPreferredSize(new Dimension(400, 400));
  11. panel.add(button[k], c);
  12.  
  13. c.gridx++;
  14.  
  15. }
Last edited by quuba; Aug 2nd, 2009 at 4:36 pm. Reason: shorter version, (but not to copy/paste)
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: msalahu is an unknown quantity at this point 
Solved Threads: 0
msalahu msalahu is offline Offline
Newbie Poster

Re: Virtual key is not working java 6

 
0
  #5
Aug 5th, 2009
Originally Posted by msalahu View Post
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
Attached Files
File Type: zip myproj.zip (44.7 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: Virtual key is not working java 6

 
1
  #6
Aug 17th, 2009
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.
  1. public GUILayer() {
  2. Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  3.  
  4. public void eventDispatched(AWTEvent event) {
  5. System.out.println("AWTEventListener:eventDispatched " + event);
  6. }
  7. }, -1);
  8. //...

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

  1. //...
  2. // Otherwise add a mouse motion listener that dispatches
  3. // a key typed event that matches the character the key
  4. // represents
  5. else {
  6. // create an anonymous class for handling the mousePressed events
  7. addMouseListener(new MouseAdapter() {
  8.  
  9. public void mousePressed(MouseEvent evt) {
  10. if (keyEventManager != null) {
  11.  
  12. keyEventManager.keyTyped(
  13. new KeyEvent(target, //VirtualKeyButton.this
  14. KeyEvent.KEY_TYPED,
  15. System.currentTimeMillis(),
  16. 0,
  17. KeyEvent.VK_UNDEFINED,
  18. theKey));
  19. }
  20. }
  21. });
  22. }
  23. }
At and realize switching through buttons with ImageIcon.


http://www.javaworld.com/javaworld/j...ng.html?page=1

http://java.sun.com/docs/books/tutor...isc/focus.html

http://tech.stolsvik.com/2009/03/awt...targeting.html
Dear Sir.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: msalahu is an unknown quantity at this point 
Solved Threads: 0
msalahu msalahu is offline Offline
Newbie Poster

Re: Virtual key is not working java 6

 
0
  #7
Aug 18th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC