Skip to content
Advertisement

JButton text changes on its own?

The following simple code:

JavaScript

can always reproduce the same bug (at least for my setups, which are given below).

The program is supposed to change the button text, according to the text-field’s text, when the button is clicked.

The problem is that the text of the button reverts/changes back to its previous value on its own unexpectedly.

The problem arises when I use the Tab to alternate/navigate between the button and the text-field. Here are the specific steps which always reproduce the same bug on my setups:

  1. Run the program.
  2. Resize the frame to be a bit bigger than it was.
  3. Type some short text in the text-field.
  4. Press Tab to navigate the focus to the button.
  5. Press Spacebar to invoke the button.
  6. Press Tab to navigate the focus back to the text-field.
  7. Type any letter you like into the text-field.

Notes:

  • I know that step 2 is relevant, because if I ommit it then the bug does not reproduce.
  • After step 2 (and before 3), the mouse should not be needed any more. Leave it. The focus of the program should be in the text-field as it was when the program launched.
  • In step 3 I usually type something like abc but the error repoduces for any other input I tried.
  • In step 6 you can also use Shift+Tab to navigate back to the text-field.
  • On step 7, after typing the first letter, you will see that the button’s text changes back to its initial/previous value.

My first tested setup:

  • Apache NetBeans IDE 8.2, which is a bit outdated.

  • java -version yields:

    java version "1.8.0_321"
    Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
    Java HotSpot(TM) Client VM (build 25.321-b07, mixed mode)
    
  • javac -version yields javac 1.8.0_161. There is a missmatch here with the runtime environment.

My second tested setup:

  • Apache NetBeans IDE 11.0.

  • java -version yields:

    java version "12.0.2" 2019-07-16
    Java(TM) SE Runtime Environment (build 12.0.2+10)
    Java HotSpot(TM) 64-Bit Server VM (build 12.0.2+10, mixed mode, sharing)
    
  • javac -version yields javac 12.0.2.

The operating system is Windows 10 on both setups.

So the question is: did I do something wrong, and if so, what is it please? First of, can anybody else reproduce the bug I am getting on their setup?

Update:

I can also reproduce this behaviour on the system L&F too, by using:

JavaScript

inside just before the JTextField creation (and catching exceptions, etc).

Advertisement

Answer

Based on the above comments I retested and now I am able to reproduce the problem. The issue occurs when you increase the vertical height of the text field (by some minimal amount).

I guess I tested before by only increasing the horizontal size of the text field.

I found a simple work around:

JavaScript

This definitely seems like a bug to me.

Having said that, rarely would you want the vertical height of the text field to increase in size. So maybe you can find a layout manager that only affects the horizontal size and not the vertical height?

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement