943,513 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 815
  • Java RSS
Aug 2nd, 2009
0

Virtual key is not working java 6

Expand Post »
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();

}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msalahu is offline Offline
7 posts
since Aug 2009
Aug 2nd, 2009
0

Re: Virtual key is not working java 6

1. add KeyListener to target
java Syntax (Toggle Plain Text)
  1. target.addKeyListener(new KeyListener() {...
or
2. use direct method
java Syntax (Toggle Plain Text)
  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);
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Aug 2nd, 2009
0

Re: Virtual key is not working java 6

Thanks a lot quuba
Click to Expand / Collapse  Quote originally posted by quuba ...
1. add KeyListener to target
java Syntax (Toggle Plain Text)
  1. target.addKeyListener(new KeyListener() {...
or
2. use direct method
java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msalahu is offline Offline
7 posts
since Aug 2009
Aug 2nd, 2009
0

Re: Virtual key is not working java 6

Is another one resolution. Just using dispatchEvent.
target not need additional listeners.
Example for button[0] and button[1]:
java Syntax (Toggle Plain Text)
  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
java Syntax (Toggle Plain Text)
  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)
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Aug 5th, 2009
0

Re: Virtual key is not working java 6

Click to Expand / Collapse  Quote originally posted by msalahu ...
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, 28 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msalahu is offline Offline
7 posts
since Aug 2009
Aug 17th, 2009
1

Re: Virtual key is not working java 6

Quote ...
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.
java Syntax (Toggle Plain Text)
  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.

java Syntax (Toggle Plain Text)
  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.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Aug 18th, 2009
0

Re: Virtual key is not working java 6

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
msalahu is offline Offline
7 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: active tombol different panel!
Next Thread in Java Forum Timeline: Pause





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC