Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTextField Issues with Numpad

I've recently run into a strange issue with the Java JTextField. When I run the following code (see below), typing a "0" into the text field first sends a paste action, then types "0". For example, if "text" is copied to the clipboard, "text0" is typed when I type "0". Similarly, typing a "4" replaces the previous character with a "4" (I'm guessing this is a delete action, then the "4" is typed). Typing "7" clears the text field before typing "7".

Here is the code:

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JTextField text = new JTextField();
    frame.add(text);
    frame.setSize(500, 500);
    frame.setVisible(true);
}

}

The problem is occurring on Red Hat Linux (accessed using VNC from Windows XP); everything runs as expected on Window XP.

Update: No problems with the program on Ubuntu either. I've also tried using different keyboards and VNC viewers.

Update 2: Java Versions

For Red Hat:

    java version "1.6.0_17"
    OpenJDK Runtime Environment (IcedTea6 1.7.7) (rhel-1.17.b17.el5-x86_64)
    OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)

For XP:

    java version "1.7.0_05"
    Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
    Java HotSpot(TM) Client VM (build 23.1-b03, mixed mode, sharing)

Update 3: Tried running the program on three different Red Hat machines (all in the same group at work), and additionally tried running it from a different XP computer and restarting.

Update 4: Today I arrived at work to find that the problem had magically gone away. However, it'd really be nice to know why it happened in the first place so that I (and anyone else who many encounter this strange issue) know how to fix it in the future.

like image 547
lrAndroid Avatar asked Jul 10 '12 17:07

lrAndroid


3 Answers

Try to put this code at the start of your program.

KeyboardFocusManager.setCurrentKeyboardFocusManager(new DefaultKeyboardFocusManager(){
    public boolean dispatchKeyEvent(KeyEvent e) {
        if (e.getKeyLocation() == KeyEvent.KEY_LOCATION_NUMPAD){
            return true;
        }
        return super.dispatchKeyEvent(e);
    }
});
like image 80
elias Avatar answered Oct 03 '22 01:10

elias


Well it is hard to give an accurate answer why but it is not really a weird phenomena. Usually when a VNC or remote desktop sharing happens, the keyboard and mouse events of one machine are transmitted to an other machine. When this mapping is done, there could be a fair chance that there might be erroneous behavior especially with the clipboard copy, pasting. It happens not just in the Linux world but also in the windows world.

I tell this by own experience. At my workplace we often rdc into other machines, some running XP and some running Windows 7. The action of clipboard copy on one machine and pasting on remote machine works on some systems and fails on others.

Quoting another such experience with java and remote desktop access, I've a java application running on my eclipse. When I rdc into my machine from some of the other machines, I find that eclipse is completely unable to launch the application. For it to work, I need to launch it on my own system first, keep the application running and then rdc from an other into mine.

Just to imagine, if this is the case with Windows XP and Windows 7 which are known belong to the same family. One can only hope that something wacky like that might not occur when using Linux and Windows together with VNC :)

As said, It is hard to be too accurate about why it is happening but it can be surely said that this is purely something that happens at the OS to OS level and not at the swing framework level.

like image 32
Vamsi Emani Avatar answered Oct 03 '22 02:10

Vamsi Emani


I'm not sure, but I'm just answering in an attempt to help:

My experience with IcedTea is bad. I can't remember exactly what happened, but back then, installing the official Java JRE solved my problems. Id est: the JRE provided by Oracle.

http://java.com/en/download/index.jsp

like image 45
Martijn Courteaux Avatar answered Oct 03 '22 01:10

Martijn Courteaux