22

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.

lrAndroid
  • 2,834
  • 18
  • 27
  • weird...sounds like it could be a keyboard mapping issue? Stupid I know, but have you made sure the numpad lock is on and works as expected in other red hat applications? – bcr Jul 10 '12 at 17:44
  • Numpad lock is on, and everything works fine when typing in other programs. I also tried using a different keyboard -- no luck. – lrAndroid Jul 10 '12 at 17:53
  • Found something here: http://www.rvdavid.net/how-to-get-the-use-of-your-number-pad-back-in-ubuntu/ It's Ubuntu of course, but could it be something similar in your case? – vaisakh Jul 10 '12 at 17:59
  • Thanks for the suggestion, but that doesn't fix it -- my numpad is still allowing me to type numbers, the problem is the actions that it sends before the numbers are typed. – lrAndroid Jul 10 '12 at 18:05
  • Is the rest of the keyboard working fine under linux? – Rorchackh Jul 14 '12 at 00:06
  • Yeah, it seems like only the numpad is having problems. – lrAndroid Jul 16 '12 at 17:30
  • What VNC client and server are you using? – 101100 Jul 16 '12 at 19:51
  • Aside from the OSs you're running, are you also running the same JREs on boths machines? – WhyNotHugo Jul 16 '12 at 19:53
  • REALVnc for the server, RealVNC/TightVnc viewer on XP. – lrAndroid Jul 16 '12 at 19:57
  • Stupid It may seem, Try restarting your machine and try again. – anthoon Jul 17 '12 at 13:29
  • I've tried it on three Linux machines (same group, at work) and restarted the XP computer twice, still having problems. – lrAndroid Jul 17 '12 at 16:59

5 Answers5

1

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);
    }
});
elias
  • 15,010
  • 4
  • 40
  • 65
1

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.

Vamsi Emani
  • 10,072
  • 9
  • 44
  • 71
1

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

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

This seems to be a known issue with VNC. According to the official VNC website:

The Num Lock key may be out of sync. Disconnect, press the client computer's Num Lock key once, and then reconnect.

Source: http://www.realvnc.com/products/viewerplus/known-issues/

This also shows up in VNC's FAQ:

Q. The keyboard doesn't work / keys do strange things!

There is one common problem which can cause this. If a modifier key, such as Shift, Ctrl or Alt, is pressed, and the viewer window then loses focus or dies, the 'key release' message never gets to the viewer and hence never gets to the remote server. The remote machine will then think that M is Ctrl-M etc. We have done various things to reduce the chance of this happening; the viewers release various modifiers automatically when they lose focus, for example, but it can still occur and can be confusing when it does. The solution is easy: simply press and release the modifier key which is stuck. If you don't know which it is, then try them one at a time.

Source: http://www-hep.nhn.ou.edu/d0/software/vnc-3.3.2r2/faq.html

If this information is indicative of your problem, then it may be that when the problem "magically disappeared," the Num Pad was simply in sync with VNC on that day and out of sync on the others (which of course means that the issue could crop up again).

Community
  • 1
  • 1
asteri
  • 11,402
  • 13
  • 60
  • 84
  • Why, then, would the issue only occur within the Java program (created via Eclipse), but not occur within any other programs? Additionally, the issue is only with numpad keys, not the entire keyboard. Furthermore, what modifier key could even cause the "0" key on the numpad to paste? – lrAndroid Jul 18 '12 at 18:32
  • So when you use the Num Pad in other applications (on Red Hat via VNC) it works fine at the same time that it is malfunctioning in the Java program? – asteri Jul 18 '12 at 18:35
  • Correct. The issue only seemed to be happening within my Java programs. – lrAndroid Jul 18 '12 at 18:38
-2

Check the "Disable application keypad mode" under Terminal, features.

hellzone
  • 5,393
  • 25
  • 82
  • 148