GUI Freezes During While Loop in Java

后端 未结 1 1889
再見小時候
再見小時候 2021-01-26 12:29

http://i.stack.imgur.com/XvHm5.png

When I click the On button it should start spamming 1\'s across the JTextField. Instead The entire GUI freezes, including the close bu

相关标签:
1条回答
  • 2021-01-26 12:47

    You have an infinite loop. The state of the toggle button is never changed after being set to true.

    Java's UI (and all UI I've worked with) is driven by a single thread. User input is fed back to you on this single thread. Rendering is also done on this single thread.

    The reason the textfield is never updated is the UI thread is never getting to render the screen again, as its tied up in your infinite loop.

    Yes, you need to put this code in a separate thread, and start it when the toggle button becomes selected. Then, you need to update the textfield from this separate thread. Look into SwingWorker.

    0 讨论(0)
提交回复
热议问题