简体   繁体   中英

Update JTextFields during an ActionPerformed in Java

I want to update 4 JTextFields one after another upon completion of some code before the button click action complete. Currently using start() and join() as shown below, but it is only updating at the end of actionPerformed. Any idea?

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();
        }

I tried swingworker and swingutilites also. but still not working.. Please help..

You should update swing components' state on ui thread(event dispatch). In your case right in the jBsendActionPerformed method, not on other threads. or call SwingUtilities.invokeLater method and update gui's state in this method when you want to update from other threads.

I think a better solution would be using swing worker.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM