```
                    if (credits >= 30) {
                        pnlSuccess.setVisible(true);

                        pnlpass.setVisible(false);


                         busdocRef.update("onbus", FieldValue.arrayUnion(txtRFID.getText()));
                         busdocRef.update("passengers", FieldValue.increment(1));

                         docRef.update("travelling", Data.busid);
                         docRef.update("slat", slat);
                         docRef.update("slon", slon);

                        txtRFID.setText("");

                        Thread.sleep(2000);


                        pnlSuccess.setVisible(false);

                        pnlpass.setVisible(true);

                        txtRFID.requestFocus();
                    }
                       ```

This si the code But the pass pnl do not hide and the success panel do not show up

Recommended Answers

All 7 Replies

Which thread is that method running on?
If it's the Swing thread (eg because it's responding to a button click) then it will hold up all Swing activity until it returns. Only after the method returns will Swing perform all those visiblity changes, and probably optimise them ot of existance anyway.

Thread.sleep is almost never the right thing to do in Java. What you should do is make the new panel visible then start a javax.swing.Timer with a 2 second delay. In the Timer's actionPerformed change the visiblities back.

It is a void function
here is the full code

            ```
            void checkPassenger() throws InterruptedException, ExecutionException {
        boolean onbus = false;
        String pasid = "0";
        String travel = "";
        String slat = "";
        String slon = "";
        String buslat = "";
        String buslon = "";
        CollectionReference cities = db.collection("passenger");
        // Create a query against the collection.
        Query query = cities.whereEqualTo("rfid", txtRFID.getText());
        // retrieve  query results asynchronously using query.get()
        ApiFuture<QuerySnapshot> querySnapshot = query.get();

    for (DocumentSnapshot document : querySnapshot.get().getDocuments()) {
        System.out.println(document.getId());
        pasid = document.getId();
        travel = document.getString("travelling");
        slat = document.getString("slat");
        slon = document.getString("slon");
        System.out.println(travel);

    }
    if (travel.equals("No") || travel.equals(Data.busid)) {

        DocumentReference docRef = db.collection("passenger").document(pasid);

        ApiFuture<DocumentSnapshot> future = docRef.get();

        DocumentSnapshot document = future.get();
        if (document.exists()) {
            double credits = document.getDouble("credits");
            System.out.println(credits);
            DocumentReference busdocRef = db.collection("bus").document(Data.busid);

            ApiFuture<DocumentSnapshot> future1 = busdocRef.get();

            DocumentSnapshot document1 = future1.get();
            if (document1.exists()) {
                onbus = false;
                buslat = document1.getString("lat");
                buslon = document1.getString("lon");
                ArrayList<String> group = (ArrayList<String>) document1.get("onbus");

                for (int a = 0; a < group.size(); a++) {
                    System.out.println(group.get(a));
                    if (group.get(a).equals(txtRFID.getText())) {
                        onbus = true;

                        break;
                    }
                }

                if (!onbus) {
                    if (credits >= 30) {
                       pnlpass.setVisible(false);

                        pnlSuccess.setVisible(true);

                        busdocRef.update("onbus", FieldValue.arrayUnion(txtRFID.getText()));
                        busdocRef.update("passengers", FieldValue.increment(1));

                        docRef.update("travelling", Data.busid);
                        docRef.update("slat", slat);
                        docRef.update("slon", slon);

                        txtRFID.setText("");


                        pnlSuccess.setVisible(false);

                        pnlpass.setVisible(true);

                        txtRFID.requestFocus();

                    } else {
                        pnlpass.setVisible(false);
                        pnlInsuff.setVisible(true);
                        txtRFID.setText("");
                        Thread.sleep(1000);

                        pnlInsuff.setVisible(false);
                        pnlpass.setVisible(true);
                        txtRFID.requestFocus();
                    }
                } else {
                    double price;
                    double dis = distance(slat, slon, buslat, buslon, 'K');
                    if (dis <= 2) {
                        price = Data.baseprice;
                    } else {
                        price = Data.baseprice + ((dis - 2) * Data.perkm);
                    }

                    double fee = Math.round(price * 100.0) / 100.0;
                    price = fee * -1;

                    ApiFuture<WriteResult> arrayRemove = busdocRef.update("onbus", FieldValue.arrayRemove(txtRFID.getText()));
                    ApiFuture<WriteResult> decrement = busdocRef.update("passengers", FieldValue.increment(-1));
                    ApiFuture<WriteResult> passfee = docRef.update("credits", FieldValue.increment(price));
                    ApiFuture<WriteResult> travellingNo = docRef.update("travelling", "No");
                    DocumentReference cred = db.collection("agent").document(Data.creditor);
                    ApiFuture<WriteResult> cd = cred.update("credits", FieldValue.increment(fee));

                    pnlpass.setVisible(false);
                    pnlSee.setVisible(true);
                    txtRFID.setText("");
                    Thread.sleep(1000);

                    pnlSee.setVisible(false);
                    pnlpass.setVisible(true);
                    txtRFID.requestFocus();

                }

            }

        }
    } else {
        if (pasid.equals("0")) {
            pnlpass.setVisible(false);
            pnlInvalid.setVisible(true);
            txtRFID.setText("");
            Thread.sleep(1000);

            pnlInvalid.setVisible(false);
            pnlpass.setVisible(true);
            txtRFID.requestFocus();
        } else {
            DocumentReference docRef = db.collection("passenger").document(pasid);
            ApiFuture<WriteResult> passfee = docRef.update("credits", FieldValue.increment(-30));
            ApiFuture<WriteResult> travellingNo = docRef.update("travelling", "No");
            DocumentReference busdocRef = db.collection("bus").document(travel);
            ApiFuture<WriteResult> arrayRemove = busdocRef.update("onbus", FieldValue.arrayRemove(txtRFID.getText()));
            ApiFuture<WriteResult> decrement = busdocRef.update("passengers", FieldValue.increment(-1));

            DocumentReference otherbus = db.collection("bus").document(travel);
            ApiFuture<DocumentSnapshot> future = otherbus.get();
            DocumentSnapshot docoldbus = future.get();
            String oldcred = docoldbus.getString("creditor");
            DocumentReference cred = db.collection("agent").document(oldcred);
            ApiFuture<WriteResult> cd = cred.update("credits", FieldValue.increment(30));
            pnlpass.setVisible(false);
            pnlAlert.setVisible(true);
            txtRFID.setText("");
            Thread.sleep(1000);

            pnlAlert.setVisible(false);
            pnlpass.setVisible(true);
            txtRFID.requestFocus();

        }
    }

}
```

The function is called on

```
   private void txtRFIDTextValueChanged(java.awt.event.TextEvent evt) {                                         
    if (txtRFID.getText().length() == 10) {
        try {
            if (txtRFID.getText().equals(Data.busRFID)) {
                Runtime runtime = Runtime.getRuntime();
                Process proc = runtime.exec("shutdown -s -t 0");
                System.exit(0);

            } else {
                checkPassenger();
            }

        } catch (InterruptedException ex) {
            Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExecutionException ex) {
            Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
```

techlifelk commented: It is a void function

That doesn't matter.

Everything to do with Swing runs on the same single thread, including all your Listeners. That also includes any screen updaing. There will be no screen updates as long as one of your listeners has the thread. So it's essential that your listeners execute and return very quickly. Sleeping in a listener will block all Swing activity until the sleep finishes and the listener returns. That's why you don't see the panels disappearing and re-appearing.

commented: Can you please give a fixing +0

Can you please suggest me a fixing
I have to stick with the logic of user inputing the id as it is using hardware reader

OK, the solution is fairly easy, and absolutely standard, so it's worth learning.

What you wanted to do is:

a.setVisible(false);
b.setVisible(true);

Thread.sleep(1000);

b.setVisible(false);
a.setVisible(true);

... but it doesn't work because Swing won't update the screen until that method returns.

So what you have to do is get rid of the sleep and use a timer, like this

    a.setVisible(false);
    b.setVisible(true);

    // start a 2 second timer with its actionPerformed as
    // {
    //    b.setVisible(false);
    //    a.setVisible(true);
    // }

    // let your method return normally. When the timer 
    // expires that will execute the actionPerformed

See the API doc for details - the example code shows what you need. (Except you want the timer to fire once only, so you will also need to call setRepeats(false) before you start the timer)

ps:

There's the same problem with calling get on an ApiFuture - you will block the Swing thread while waitig for the future to complete. In this case you may not be bothered by that, but in general it's a bad idea.

commented: I removed ith API Future but i am still confused on how to apply the timer could you please apply it for just one. Thank you +0

I don't like posting code for people to copy without understanding, so please do take the time to read and understand this in conjunction with my previous posts before copying it...

a.setVisible(false);
b.setVisible(true);
// create a timer to call its ActionListener after 1000mSec
Timer t = new Timer(1000, new ActionListener() {
       public void actionPerformed(ActionEvent evt) {
            b.setVisible(false);
            a.setVisible(true);
       }
  });
t.setRepeats(false);
t.start();

you'll need to import javax.swing.Timer and maybe java.awt.event.ActionListener

commented: Thank you a lot +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.