簡體   English   中英

如何從 CompletableFuture 中設置文本

[英]How to setText from a CompletableFuture

我在通過 completableFuture 設置窗口名稱時遇到問題。 我需要獲取用戶所在的帳戶,並以此命名主要的 window。

MainWindow mainWindow = this;
...
CompletableFuture.supplyAsync(() -> {
        while(true) {
            try {
                // wait until the account is chosen
                if (MainWindow.idAccount > 0 ) {
                    ResultSet rs = // some request to the db
                    rs.next();
                    return rs.getString(1);
                }
            } catch (Exception e) {
                System.out.println("Error while getting the address of the account. " + e.getMessage());
            }
        }
    }).thenAccept(account -> {
        System.out.println(account);
        mainWindow.setText("POS - " + account);
    });

一切正常,實際上在.thenAccept() 控制台打印出正確的帳戶地址。 但是,文本從未設置過。

可能與線程之間的訪問有關嗎? 提前致謝。

我假設 MainWindow 是一個 Swing 組件。 Swing 組件在 EDT 中更新。

SwingUtilities.invokeLater(() -> mainWindow.setText("POS - " + account));

附注:如果它是一個框架,您可以嘗試使用setTitle設置標題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM