9

I have banking gui application that I am currently working on and there seems to be a problem with the setvisible method for my jdialog. After the user has withdrawn a valid amount I pop up a simple dialog that says "transaction in progress". In my dobackground method i keep polling to check if the transaction has been received. I tried using swingworker and I don't understand why it's not working. If i remove the setvisible call it works fine, so why does setvisible cause the system to hang? Here is the code that is inside my jbutton mouselistener:

SwingWorker<String,Integer> worker = new SwingWorker<String,Integer>(){

  JDialog waitForTrans = new JDialog((JFrame)null,true);
  public String doInBackground() throws Exception {
     waitForTrans.add(new JLabel("Updating balance in system. Please Wait..."));
     waitForTrans.setMinimumSize(new Dimension(300,100));
     waitForTrans.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     waitForTrans.setVisible(true);
     Bank.getInstance().sendTransaction(currentPin,"-"+withdraw);
     while(!Bank.getInstance().hasCompletedTransaction){

     }
     return null;

  }

  public void done(){
   try {
        this.get();
       } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {

        e.printStackTrace();
    }
    waitForTrans.setVisible(false);
    newField.setText(String.valueOf(Bank.getInstance().getAccountList().get(currentPin).getBalance()));
  }

 };
 worker.execute();
WhiteKnight
  • 4,938
  • 5
  • 37
  • 41
Adith
  • 165
  • 2
  • 2
  • 5
  • What version of java did you see this blocking? We upgraded from java 6 to java 8 and might be running into this behavior. – simgineer Dec 28 '17 at 20:00

4 Answers4

15

First, it is recommended to do all the GUI updates in the Swing Event-Dispatch thread, i.e. using the SwingUtilites class.

Second, your JDialog is modal and so blocks the thread in which the setVisible(true) method is called (in your case the Main thread, in the following case the Swing Event-Dispatch Thread).

I do not say the following code is perfect, but it should put you on the track...


final JDialog waitForTrans = new JDialog((JFrame) null, true);

SwingWorker worker = new SwingWorker() {

  public String doInBackground() throws Exception {
    Thread.sleep(5000);
    return null;
  }

  public void done() {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        waitForTrans.setVisible(false);
        waitForTrans.dispose();
      }
    });
  }

};

worker.execute();
SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    waitForTrans.add(new JLabel("Please Wait..."));
    waitForTrans.setMinimumSize(new Dimension(300, 100));
    waitForTrans.setVisible(true);
  }
});

Hope this helps.

Jiri Patera
  • 3,140
  • 1
  • 20
  • 14
  • Thanks for the quick responses! I am still a bit shaky on these kind of aspects to gui programming but I get it for the most part now... – Adith Dec 10 '10 at 22:01
  • Understanding to how the Swing Event-Dispatch Thread works and how to use it in the right way was the hardest part of the Swing GUI for me. Take your time and get it right, it will save you a lot of trouble later on. – Jiri Patera Dec 10 '10 at 22:05
  • 1
    The done method is executed on the Event Dispatch Thread so I don't think you need to use SwingUtilities.invokeLater in that method. See http://docs.oracle.com/javase/6/docs/api/javax/swing/SwingWorker.html#done%28%29 – Ryan May 06 '14 at 20:56
  • 1
    Also this answer has a race condition: it is possible for the done method to be called and the dialog destroyed before the dialog is set to visible. – Ryan May 06 '14 at 21:33
  • I may be missing something, I was focusing mainly only on the OP's problem, but from my point of view the race condition, you are mentioning, is not possible here. You set the dialog visible via the code here BEFORE you call the `done()` method. The reason is that both the actions are done in a serial way in the Swing Event-Dispatch thread. The use of the `SwingUtilities.invokeLater(Runnable)` method posts the actions to the thread in a synchronized way, i.e. no race condition you described is possible here. – Jiri Patera May 14 '14 at 07:45
6

You are displaying a modal dialog so the background code can't execute until the dialog is closed.

Add a System.out.println(...) statement after the setVisible and you will see it never executes.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

setVisible is a method that affects the GUI, causing something to be shown (and, in the case of a modal dialog like yours, block until the dialog is closed). It (like everything else that modifies the visible UI) should never be called except on the Swing event dispatch thread. You're calling it from the doInBackground method of SwingWorker, which runs on a background thread.

What you need to do to fix this is make the waitForClose dialog a final variable that you create before calling execute on the SwingWorker and then call setVisible on immediately after starting the worker.

final JDialog waitForTrans = ...
// set up the dialog here

SwingWorker<String, Integer> worker = new SwingWorker<String, Integer>() {
  ...
};
worker.execute(); // start the background process

waitForTrans.setVisible(true); // show the dialog

You need to do it in this order because otherwise the modal dialog will block you from starting the worker.

ColinD
  • 108,630
  • 30
  • 201
  • 202
0

camickr gives you correct answer. I want to add that you may not modify the UI outside the Event Dispatch Thread (as you do in #doInBackground), Swing is single threaded so violating this rule could lead to very tricky bugs and strange things in your UI.

Uhlen
  • 1,778
  • 15
  • 29