2

I have a Java program used by my contributors and I need to update it to work on newer Windows versions. I had to decompile the existing one to get the code. One of the bugs is that one of the fields on the GUI is set up as a digit-only field, and then when you hit the clear button, all of the text fields get cleared except for the text field. Here's how the digit-only restriction has been coded:

public void keyPressed(KeyEvent paramKeyEvent)
{
  char c = paramKeyEvent.getKeyChar();
  if ((!Character.isDigit(c)) && (Character.getType(c) != Character.CONTROL)) {
    paramKeyEvent.consume();
  }
}

When the clearing code writes to the text field using setText("") executes, the field is not cleared. I suspect it is because the null is not recognized by the above if statement and the event is consumed.

I programmed in C for 15 years, but I've only been teaching myself Java since January, so I'm not able to make major changes to this code in a timely manner at this point. I see that there are many ways to implement the digit-only input, but I'm not eager to rewrite large portions of this code if at all possible to resolve it the way it is structured.

I appreciate any help in figuring this out. Thanks.

As an update, I tried writing a zero to the field instead of "" and that works, so it does seem like it's the way the digit-only field is implemented that is causing the problem.

After more searching (I thought I'd exhaustively searched!) I found reference to a setText bug -- the only way around it seems to be to use setText to write a blank and then null. It works!

ajrjaneway
  • 31
  • 3

1 Answers1

0

You cannot do setText("") instead add a blank, setText(" ")

  • Well, that's the weird thing -- all the other text fields work just fine writing "". It's only the field that is digit-only that will not clear. But I did discover as you say, that if I write a blank and then a "", it works. – ajrjaneway Mar 21 '16 at 03:11