0

I created a JDialog which I want to move and resize. My program draws JDialog on the screen. When the user clicks it, it should stretch to the width of the screen and then gain height. I tried it like this.

for(int i = 150; i <= width; i += 3) {
    dialog.setSize(i, 80);

    try {
        Thread.sleep(0, 1);
    } catch(Exception e2) {}
}

for(int i = 80; i <= 200; i++) {
    dialog.setSize(width, i);

    try {
        Thread.sleep(1);
    } catch(Exception e3) {}
}

When the code is executed, it will take a while and then the JDialog will be shown stretched immediately. No expanding is shown.

Well, when the user clicks the dialog again, it will reverse the opening animation and close.

for(int i = 200; i >= 80; i--) {
    newMsg.setSize(width, i);

    try {
        Thread.sleep(0, 1);
    } catch(Exception e4) {}
}   

for(int i = 0; i >= -width; i -= 3) {
    newMsg.setLocation(i, 100);

    try {
        Thread.sleep(0, 1);
    } catch(Exception e5) {}
}

This one works correctly. The movement is able to be seen. As far as I understand, these codes are otherwise identical except they are reversed. Why doesn't the opening work as expected but the closing does?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MikkoP
  • 4,864
  • 16
  • 58
  • 106
  • Try using a swing Timer rather than a loop. I think the events are just getting stuck on the event queue and then all executing at once. – DankMemes Dec 30 '12 at 17:49
  • I'd like to see how you would do it. I commented Reimeus' message and explained... – MikkoP Dec 30 '12 at 18:26
  • possible duplicate of [using sleep() for a single thread](http://stackoverflow.com/questions/14074329/using-sleep-for-a-single-thread) – David Kroukamp Dec 31 '12 at 05:15

1 Answers1

4

Don't call Thread.sleep in the EDT. This causes the UI to "freeze". You could use a Swing Timer here instead. Here is how the initial "expand" might be handled::

Timer timer = new Timer(0, new ActionListener() {

   @Override
   public void actionPerformed(ActionEvent e) {
    updateDialogSize();
   }
});

timer.setDelay(5); // short delay
timer.start();

Update dialog:

void updateDialogSize() {
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   if (dialog.getWidth() < screenSize.getWidth()) { // 1st size width
      dialog.setSize(dialog.getWidth() + 5, dialog.getHeight());
   } else if (dialog.getHeight() < screenSize.getHeight()) { // then size height
     dialog.setSize(dialog.getWidth(), dialog.getHeight() + 5);
   } else {
      timer.stop(); // work done
   }

     dialog.setLocationRelativeTo(myFrame);
}

I'll leave the dialog "contraction" as an excercise ;)

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Please provide some code. I can't figure out how this should be done. I tried to do it with the Timer, but I didn't quite get it... I need multiple timers which forces me to define the ActionListener inside the Timer initialization which in turn makes me unable to stop the timer inside the ActionListener. – MikkoP Dec 30 '12 at 18:08