在Java中的ActionPerformed期间更新JTextFields

时间:2017-04-06 16:01:56

标签: java multithreading swing user-interface concurrency

我想在按钮点击操作完成之前完成一些代码后一个接一个地更新4个JTextField。目前使用start()和join()如下所示,但它只在actionPerformed结束时更新。有什么想法吗?

public void jBsendActionPerformed(ActionEvent e) {
            try{
        selectedPort=jCports.getSelectedItem().toString();

        //Thread t1;
        Thread t1 = new Thread(new Runnable() {
            @Override
            public synchronized void run() {
                try {
                    String data = "*"+jTx.getText()+jTy.getText()+jTx1.getText()+jTy1.getText()+jTl1.getText()+jTb1.getText()+"#";
                    obj.writeData(selectedPort, data);
                    String RFID1=obj.readData(selectedPort);
                    jTrfid1.setText(RFID1); // Want to update this textfield before executing the rest of the code, same for the next three textfields
                    System.out.println(RFID1);
                } catch (SerialPortException ex) {
                    Logger.getLogger(RoboGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            } 
        });

        t1.start();
        t1.join();
        Thread t2 = new Thread(new Runnable() {
            @Override
            public synchronized void run() {
                try {
                    String data = "*"+jTx2.getText()+jTy2.getText()+jTl2.getText()+jTb2.getText()+"#";
                    obj.writeData(selectedPort, data);
                    String RFID2=obj.readData(selectedPort);

                } catch (SerialPortException ex) {
                    Logger.getLogger(RoboGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            } 
        });

        t2.start();
        t2.join();
        Thread t3 = new Thread(new Runnable() {
            @Override
            public synchronized void run() {
                try {
                    String data = "*"+jTx3.getText()+jTy3.getText()+jTl3.getText()+jTb3.getText()+"#";
                    obj.writeData(selectedPort, data);
                    String RFID3=obj.readData(selectedPort);
                    jTrfid3.setText(RFID3);
                    System.out.println(RFID3);
                } catch (SerialPortException ex) {
                    Logger.getLogger(RoboGUI.class.getName()).log(Level.SEVERE, null, ex);
                }
            } 
        });
        t3.start();
        t3.join();
        Thread t4 = new Thread(new Runnable() {
            @Override
            public synchronized void run() {
                try {
                    String data = "*"+jTx4.getText()+jTy4.getText()+jTl4.getText()+jTb4.getText()+"#";
                    obj.writeData(selectedPort, data);
                    String RFID4=obj.readData(selectedPort);
                    jTrfid4.setText(RFID4);
                    System.out.println(RFID4);
                } catch (SerialPortException ex) {

                }
            } 
        });
        t4.start();
        t4.join();
        }

我也尝试过swingworker和swingutilites。但仍然没有工作..请帮助..

1 个答案:

答案 0 :(得分:3)

你应该在ui线程上更新swing组件的状态(事件调度)。在你的情况下,在jBsendActionPerformed方法中,而不是在其他线程上。或者调用SwingUtilities.invokeLater方法,并在想要从其他线程更新时更新此方法中的gui状态。

我认为更好的解决方案是使用摇摆工作者。

public void jBsendActionPerformed(ActionEvent e) {
    selectedPort=jCports.getSelectedItem().toString();
    final String data = "*"+jTx.getText()+jTy.getText()+jTx1.getText()+jTy1.getText()+jTl1.getText()+jTb1.getText()+"#",
                 data1 = "*"+jTx2.getText()+jTy2.getText()+jTl2.getText()+jTb2.getText()+"#",
                 data2 = "*"+jTx3.getText()+jTy3.getText()+jTl3.getText()+jTb3.getText()+"#",
                 data3 = "*"+jTx4.getText()+jTy4.getText()+jTl4.getText()+jTb4.getText()+"#";
    new SwingWorker<Void, String>() {
        int messageInd = 0;

        @Override
        protected Void doInBackground() throws Exception {
            //will be called on worker thread
            try {
                obj.writeData(selectedPort, data);
                String RFID1=obj.readData(selectedPort);
                push(RFID1);
                System.out.println(RFID1);
            } catch (SerialPortException ex) {
                Logger.getLogger(RoboGUI.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                obj.writeData(selectedPort, data1);
                String RFID2=obj.readData(selectedPort);
                push(RFID2);    
                System.out.println(RFID2);
            } catch (SerialPortException ex) {
                Logger.getLogger(RoboGUI.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                obj.writeData(selectedPort, data2);
                String RFID3=obj.readData(selectedPort);
                push(RFID3);
                System.out.println(RFID3);
            } catch (SerialPortException ex) {
                Logger.getLogger(RoboGUI.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
                obj.writeData(selectedPort, data3);
                String RFID4=obj.readData(selectedPort);
                push(RFID4);
                System.out.println(RFID4);
            } catch (SerialPortException ex) {

            }
            return null;
        }

        @Override
        protected void process(List<String> rfids) {
            //will be called on ui thread
            for(String rfid : rfids) {
                if( messageInd==0 )
                    jTrfid1.setText(rfid);
                else if( messageInd==1 )
                    jTrfid2.setText(rfid);
                else if( messageInd==2 )
                    jTrfid3.setText(rfid);
                else if( messageInd==3 )
                    jTrfid4.setText(rfid);
                messageInd++;
            }
        }
    }.execute();
}