JLabel not refreshing without needing additional threads?

CodeSammich

I'm making a countdown timer which refreshes every second.

I can't figure out for the life of me how to make the JLabel refresh. I've searched other threads, but they all involve using additional threads. Is there a way to make either the JLabel/Panel refresh? (Panel may be unnecessary). It works if I remove the while loop, which would just give me a still String of the timer at its original time, with no decrement. But if I include the while loop, it just returns a constant blank screen until the timer runs out, then it goes back to the original value.

How should I refresh the JLabel so that it will display to the GUI every time it decrements (hopefully without using threads..) ? (Or every time the while loop runs)

MadProgrammer

"It works if I remove the while loop"

You're blocking the event dispatching thread.

Swing is a single threaded framework, that means that anything the blocks the EDT will prevent from processing the Event Queue and cause the UI to hang.

See Concurrency in Swing for more details.

The simplest solution would be to use a Swing Timer, which will allow you to schedule a regular callback which will be executed from within the context of the EDT.

See How to use Swing Timers for more details

javax.swing.Timer timer = new javax.swing.Timer(500, new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        counter++;
        label.setText(count);
    }
});

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Implementing a refreshing table with threads

分類Dev

Return rows from INSERT with ON CONFLICT without needing to update

分類Dev

How to use a Twitter stream source in Hazelcast Jet without needing a DAG?

分類Dev

Keeping QPlainTextEdit as small as possible without needing a scroll bar

分類Dev

Django live Insert without refreshing the page

分類Dev

Show added posts without refreshing page in React

分類Dev

PHP email form without Refreshing Page

分類Dev

Submit an action without refreshing or redirecting the page

分類Dev

JPane "paintComponent" can't be called without JLabel

分類Dev

adding additional RAM without reinstalling windows

分類Dev

Delimiting some sibling nodes without additional element

分類Dev

It is possible to use the TessAPI1.TessPDFRendererCreate API of tess4J without needing to create physical files?

分類Dev

Specify the dots argument when calling a tidyselect-using function without needing to specify the preceding arguments

分類Dev

Proper way to serve updated files without user refreshing on nginx

分類Dev

Add new option to select list without refreshing the page

分類Dev

How to call controller's methods without refreshing the web page?

分類Dev

How to correctly display data stored in an external model without refreshing the page?

分類Dev

Updates the data in vue with laravel without refreshing the page or overlapping the div

分類Dev

Update MySQL table with button without refreshing page using ajax

分類Dev

Sublime Text - Run projects on apache tomcat without refreshing eclipse project

分類Dev

displaying <select> options after inserting record without refreshing the page

分類Dev

Server-side show/hide of HTML without producing additional markup

分類Dev

Why the call method to runloop.run in the Mac OS console application creates additional threads?

分類Dev

React Flash Message: How to make the message show without refreshing the page but refresh on 200

分類Dev

JQuery UI Sortable + DataTables show all rows without refreshing current page

分類Dev

How to wait for multiple threads to finish without blocking ui in android?

分類Dev

Refreshing a UICollectionview

分類Dev

JLabel is disappearing

分類Dev

Firestore: get document back after adding it / updating it without additional network calls

Related 関連記事

  1. 1

    Implementing a refreshing table with threads

  2. 2

    Return rows from INSERT with ON CONFLICT without needing to update

  3. 3

    How to use a Twitter stream source in Hazelcast Jet without needing a DAG?

  4. 4

    Keeping QPlainTextEdit as small as possible without needing a scroll bar

  5. 5

    Django live Insert without refreshing the page

  6. 6

    Show added posts without refreshing page in React

  7. 7

    PHP email form without Refreshing Page

  8. 8

    Submit an action without refreshing or redirecting the page

  9. 9

    JPane "paintComponent" can't be called without JLabel

  10. 10

    adding additional RAM without reinstalling windows

  11. 11

    Delimiting some sibling nodes without additional element

  12. 12

    It is possible to use the TessAPI1.TessPDFRendererCreate API of tess4J without needing to create physical files?

  13. 13

    Specify the dots argument when calling a tidyselect-using function without needing to specify the preceding arguments

  14. 14

    Proper way to serve updated files without user refreshing on nginx

  15. 15

    Add new option to select list without refreshing the page

  16. 16

    How to call controller's methods without refreshing the web page?

  17. 17

    How to correctly display data stored in an external model without refreshing the page?

  18. 18

    Updates the data in vue with laravel without refreshing the page or overlapping the div

  19. 19

    Update MySQL table with button without refreshing page using ajax

  20. 20

    Sublime Text - Run projects on apache tomcat without refreshing eclipse project

  21. 21

    displaying <select> options after inserting record without refreshing the page

  22. 22

    Server-side show/hide of HTML without producing additional markup

  23. 23

    Why the call method to runloop.run in the Mac OS console application creates additional threads?

  24. 24

    React Flash Message: How to make the message show without refreshing the page but refresh on 200

  25. 25

    JQuery UI Sortable + DataTables show all rows without refreshing current page

  26. 26

    How to wait for multiple threads to finish without blocking ui in android?

  27. 27

    Refreshing a UICollectionview

  28. 28

    JLabel is disappearing

  29. 29

    Firestore: get document back after adding it / updating it without additional network calls

ホットタグ

アーカイブ