| | |
Virtual key is not working java 6
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
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();
}
}
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();
}
}
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
1. add KeyListener to target
or
2. use direct method
java Syntax (Toggle Plain Text)
target.addKeyListener(new KeyListener() {...
2. use direct method
java Syntax (Toggle Plain Text)
// 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)); } }); } }
Last edited by quuba; Aug 2nd, 2009 at 1:46 pm. Reason: added line:target.setText(s);
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
Thanks a lot quuba
•
•
•
•
1. add KeyListener to target
orjava Syntax (Toggle Plain Text)
target.addKeyListener(new KeyListener() {...
2. use direct method
java Syntax (Toggle Plain Text)
// 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)); } }); } }
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
Is another one resolution. Just using dispatchEvent.
target not need additional listeners.
Example for button[0] and button[1]:
or shorter version
target not need additional listeners.
Example for button[0] and button[1]:
java Syntax (Toggle Plain Text)
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
java Syntax (Toggle Plain Text)
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++; }
Last edited by quuba; Aug 2nd, 2009 at 4:36 pm. Reason: shorter version, (but not to copy/paste)
•
•
Join Date: Aug 2009
Posts: 7
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
•
•
•
•
Infact this code was written in jdk1.2.2 and its working well there
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)
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.
java Syntax (Toggle Plain Text)
//... // 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)); } } }); } }
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.
![]() |
Similar Threads
- Sony Vaio Function Key On Keyboard Not Working. (USB Devices and other Peripherals)
- Junior Swing Java Developer (Software Development Job Offers)
- Hello! Team working on a java project for university (Java)
- Help! Fn key not working on Vaio PCG-K64 (Troubleshooting Dead Machines)
- JAVA Developer wanted ASAP!! (Software Development Job Offers)
- Primary Key pls help! (Java)
- Delete button not working on my Java GUI Inventory (Java)
- Barcode Readers and Java (Java)
- Very New to java, skeleton of program given (Java)
Other Threads in the Java Forum
- Previous Thread: active tombol different panel!
- Next Thread: Pause
| Thread Tools | Search this Thread |
Tag cloud for Java
actionlistener android api apple applet application apps arguments array arrays automation balls binary bluetooth card chat class classes clear client code component consumer database draw eclipse ee error event exception fractal free game gameprogramming gis givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javaprojects jni jpanel julia jvm key linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie nextline nls notdisplaying number oracle output print problem program programming project recursion recursive scanner screen security server set size sms socket sort spamblocker sql sqlite string sun swing terminal test threads time tree trolltech windows





