[JFrame with TextFields error] [SOLVED]

SOLVED: I’m lame sorry guys:


	public void keyPressed(KeyEvent e) {
		int ID = e.getKeyCode();
		if (ID == 8) { /* Backspace Button */
			if (userNameField.getText().length() == 0) {
				e.consume();
			}
		}
	}

Hello JGO :o
I have a JFrame, it contains 2 JTextField’s, and multiple JPanel’s (JPanel’s are painting constantly).

Whenever I have one of the TextField’s selected, if I hit ‘back space’ it causes my JPanel’s paint rate to significantly speed up?

Picture:

Note: While I hit backspace, this glitch only occurs while there’s NO text in the field.

I could supply some source code, if need be.
(How I noticed this was that the physics of the spheres would speed up)

I know you already solved this, but I suggest using the [icode]KeyEvent[/icode] constants for better readability:


   public void keyPressed(KeyEvent e) {
      int ID = e.getKeyCode();
@@      if (ID == KeyEvent.VK_BACK_SPACE) { /* Backspace Button */
         if (userNameField.getText().length() == 0) {
            e.consume();
         }
      }
   }

[icode]KeyEvent.VK_BACK_SPACE[/icode] is exactly this in source code: [icode]public static final int VK_BACK_SPACE = 8;[/icode].

I guess you already know this… I’m just sayin, because it’s better readable :slight_smile:

Thanks, I know a couple java constants which I use alot, easier to just write the digit XD:


3
vs:
JFrame.DEFAULT_CLOSE_OPERATION

This was and still is a rather interesting glitch though.
On a JTextField/JPasswordField/JTextArea, if while it’s blank the user hits ‘Backspace’ or ‘Del’, it causes all the JPanels on the same JFrame to tweak out lol, compare it like a steady FPS rate of 2, which increases to 100 FPS.
Wierd :stuck_out_tongue:

Using magic numbers defeats the purpose of the constants in the first place; it makes your code harder to read and more difficult to debug, and also makes it much more error prone.

Instead you should use IDE auto-completion if you are getting tired of typing things out.

You will probably come to realize the importance as you begin to make more complex applications.

Lolz, I used IDE auto-completion to find out the actual variable value in the first place.
If I find out it’s ‘one’ character long in length, why would I type 20+ characters?
I said I know a couple of constants lol, and I use them quite often, wouldn’t type the longer version for somebody else s eyes.
I’m the only one browsing my code for the most part O.O

Do you give your classes and methods single-character names as well? It would make your code even more terse!

Guys, keep cool.

If he wants to do it that way, let him just do it that way. You learn from mistakes.

Would you type:


ch.setLocalTranslation(settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2, settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);

Or this:


ch.setLocalTranslation(x, y, 0);

So yes noob, I do name ‘variables’ with 1 letter sometimes, maybe you should try it, stop criticizing lulz.
As for method names no rofl, how did it get to method names, starting from ‘known’ constants?