簡體   English   中英

使用線程更新JLabel

[英]Updating JLabel with thread

我試圖讓一個線程在單擊按鈕時為swing應用程序運行,但是該值沒有更新。 它應該獲取我正在搜索的計算機名稱,但是為了更新該值,我必須啟動一個新的GUI實例。 我創建了一個線程,但是由於某種原因它無法正常工作。 任何幫助表示贊賞。

(t.start在代碼塊的末尾)

searchComputerButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
              Thread t = new Thread("my non EDT thread") {
                    public void run() {
                        //my work
                        testLabel.setText(CN);
                    }

                };

            String line;
            BufferedWriter bw = null;
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new FileWriter(tempFile));
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // String lineToRemove = "OU=Workstations";

            String s = null;

            Process p = null;
            /*
             * try { // p = Runtime.getRuntime().exec(
             * "cmd /c start c:\\computerQuery.bat computerName"); } catch
             * (IOException e1) { // TODO Auto-generated catch block
             * e1.printStackTrace(); }
             */
            try {

                p = Runtime.getRuntime().exec("c:\\computerQuery.bat");

            } catch (IOException e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            }
            StringBuffer sbuffer = new StringBuffer();
            BufferedReader in = new BufferedReader(new InputStreamReader(p
                    .getInputStream()));

            try {

                while ((line = in.readLine()) != null) {

                    System.out.println(line);

                    // textArea.append(line);

                    String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
                    LdapName ldapName = new LdapName(dn);
                    String commonName = (String) ldapName.getRdn(
                            ldapName.size() - 1).getValue();

                }
                ComputerQuery.sendParam();

            } catch (IOException e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            } catch (InvalidNameException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally

            {
                try {
                    fw.close();

                }

                catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

            try {

                in.close();

            } catch (IOException e1) {

                // TODO Auto-generated catch block

                e1.printStackTrace();

            }

            ComputerQuery.sendParam();
            t.start();
        }
    });

UPDATE

private void threadStart() {
          SwingUtilities.invokeLater(new Runnable() {
            public void run() {

              testLabel.setText(CN);
            }
          });

我把方法放在這里

JButton searchComputerButton = new JButton("Search");
        searchComputerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                threadStart();
                String line;

注意Swing線程https://docs.oracle.com/javase/tutorial/uiswin/concurrency/

在這里看看: http : //www.javamex.com/tutorials/threads/invokelater.shtml

您必須使用方法SwingUtilities.invokeLater(???)使JLabel更新方法調用排隊。 下面的例子做到了

此外,我認為這與.batch文件調用有關。 在這里看看: 如何從Java應用程序運行批處理文件? 可運行任務= new UpdateJob(“ Query:” + i); SwingUtilities.invokeLater(任務);


使它更易於理解。

Swing在一個線程內管理所有繪圖操作。 它提供一個隊列。 如果從該隊列之外調用方法,則行為是完全不可預測的。NullPointer ... RuntimeExc ....但是,如果調用SwingUtilities.invokeLater(...),則該方法將排隊進入Swing-Queue並被調用盡快地!

由於評論更新:

檢查您的主線程(GUI)檢查您的線程。 當子線程(例如ActionListener)要調用JLabel :: setText時,必須使用SwingUtils :: InvokeLater(“ ...”)方法;

這意味着必須在不直接屬於主線程的所有線程中調用invokeLater()

由於問題而更新

在我看來,您當前不需要的代碼根本不需要SwingUtilities.invok..。 您是否更改了分配給您的問題的代碼。

暫無
暫無

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

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