Setvisible method in java swing system freezes

I have a banking gui application that I am working on now and there seems to be a problem with the setvisible method for my jdialog. After the user has withdrawn the actual amount, I open a simple dialog that says "the transaction is ongoing." In my dobackground method, I continue to poll to check if a transaction has been received. I tried using swingworker and I do not understand why it does not work. If I delete 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();
+5
source share
4

-, Swing Event-Dispatch, .. SwingUtilites.

-, JDialog , setVisible(true) ( , - Swing Event-Dispatch Thread).

, , ...


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

, .

+13

, .

System.out.println(...) setVisible, , .

+5

setVisible - , , - ( , , ). ( , ) , Swing. doInBackground SwingWorker, .

, , waitForClose a final, , execute SwingWorker, setVisible .

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

, .

+1

camickr gives the correct answer. I want to add that you cannot change the user interface outside the flow of event messages (as you do in #doInBackground), Swing is single-threaded, so violating this rule can lead to very complex errors and strange things in the user interface.

0
source

All Articles