Click to See Complete Forum and Search --> : Can't dispose my dialog after moving it using swing timer
eranga262154
Apr 8th, 2008, 10:17 PM
Hi all,
I use the swing timer to move a dialog from one position to another. Here is the code I have use.
// Set the main window display location
public void setWindowLocation()
{
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int frameHeight = this.getHeight();
int frameWidth = this.getWidth();
int _xValue = (screenWidth - frameWidth);
int _yValue = (screenHeight - getTrayheight() );
int _yEnd = (screenHeight - frameHeight - getTrayheight() );
//this.setLocation((screenWidth - frameWidth), (screenHeight - frameHeight - getTrayheight() - WINDOW_SPACE));
this.setLocation(_xValue, _yValue);
while(_yValue > _yEnd){
dialogMotion(this, _xValue, _yValue);
_yValue -= 1;
}
// this.dispose();
}
// Dialog motion
private void dialogMotion(final MainDialog mainDialog, final int x, final int y){
AbstractAction abAction = new AbstractAction() {
public void actionPerformed(ActionEvent evnt) {
mainDialog.setLocation(x, y);
}
};
Timer motionTimer = new Timer(300, abAction);
motionTimer.setRepeats(false);
motionTimer.start();
}
// Find the taskbar height to place the main frame in lower-right corner
// of the viewable area of the screen.
private static int getTrayheight(){
return (Toolkit.getDefaultToolkit().getScreenSize().height -
GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
}
I think my code is clear to you. After moving the dialog onto the task bar, I want to dispose it. You can see that at the end of the while loop I've try it.
What happened there is, I can't see the dialog moving. it directly dispose. I add a breakpoint at the while loop and check, it iterate until condition is false. But I can't see the motion. If I comment the dispose code line as in my above code, I can see it moving.
Why is that. Your comment really helpful to me.
I'm worried is that debugging iterate that while loop but it is not visible. :( :(
ComputerJy
Apr 8th, 2008, 11:01 PM
I don't know what are you talking about. I can't see any calls to the dispose method in your code, except for the commented code in the method never called
eranga262154
Apr 8th, 2008, 11:05 PM
Yes that's the line I talking about. Say I remove the comments on that code line
this.dispose();
So what happened there is, dialog is dispose suddenly without moving it. If I add the comments, dialog moves nicely.
ComputerJy
Apr 8th, 2008, 11:30 PM
Finally got it to work :)
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class JDialogNotification extends JDialog {
private static final long serialVersionUID = 9077763988467568226L;
private static int getTrayheight() {
return (Toolkit.getDefaultToolkit().getScreenSize().height - GraphicsEnvironment
.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JDialogNotification dialog = new JDialogNotification(
new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
public JDialogNotification(Frame owner, boolean modal) {
super(owner, modal);
initComponents();
setWindowLocation();
}
// Dialog motion
private void dialogMotion(final JDialog mainDialog, final int x, final int y) {
AbstractAction abAction = new AbstractAction() {
private static final long serialVersionUID = -1005676924292901160L;
public void actionPerformed(ActionEvent evnt) {
mainDialog.setLocation(x, y);
}
};
Timer motionTimer = new Timer(300, abAction);
motionTimer.setRepeats(false);
motionTimer.start();
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 138,
Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 112,
Short.MAX_VALUE));
pack();
}
// Set the main window display location
public void setWindowLocation() {
try {
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int frameHeight = this.getHeight();
int frameWidth = this.getWidth();
int _xValue = screenWidth - frameWidth;
int _yValue = screenHeight - getTrayheight();
int _yEnd = screenHeight - frameHeight - getTrayheight();
// this.setLocation((screenWidth - frameWidth), (screenHeight -
// frameHeight - getTrayheight() - WINDOW_SPACE));
this.setLocation(_xValue, _yValue);
while (_yValue > _yEnd) {
dialogMotion(this, _xValue, _yValue);
_yValue -= 1;
}
Thread.sleep(1000);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.sleep(1500);
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} catch (InterruptedException ex) {
Logger.getLogger(JDialogNotification.class.getName()).log(
Level.SEVERE, null, ex);
}
}
}
eranga262154
Apr 8th, 2008, 11:46 PM
Thanks for the replay. I'm worried about that what is the usage of long value serialVersionUID in your code? Can you explain a little more that.
ComputerJy
Apr 8th, 2008, 11:59 PM
Ah it's nothing, doesn't affect this application at all
eranga262154
Apr 9th, 2008, 12:57 AM
It's ok pal. May it's because of my bad explanation.
I have another question too. Using your code, I try to move the dialog in other way round you have done. Initially the dialog is on the tray, and I move into below of the tray.
So I add a button to the form and use the same way to do it. Here is the code.
private void hideWindow(MainDialog mainGialog){
try{ /// VBP - this line only
int _xAbove = mainGialog.getLocation().x;
int _yAbove = mainGialog.getLocation().y;
int _yBelow = mainGialog.getLocation().y + mainGialog.getHeight() + getTrayheight();
while(_yBelow > _yAbove){
hideWindowMotion(this, _xAbove, _yAbove);
_yAbove += 1;
}
Thread.sleep(1000);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.sleep(1500);
//System.exit(0);
dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
private void hideWindowMotion(final MainDialog mainDialog, final int x, final int y){
AbstractAction abAction = new AbstractAction() {
public void actionPerformed(ActionEvent evnt) {
mainDialog.setLocation(x, y);
}
};
Timer motionTimer = new Timer(300, abAction);
motionTimer.setRepeats(false);
motionTimer.start();
}
What happened here is after click the button, dialog holds/freeze a little time that same to thread sleep and then start move. After that it dispose. Why it is happened. As far as I get there, it's happened on the Thread sleep. I can't reduce the sleep time also.
If I reduce the sleep time same scenario take there, not moving the dialog and just it's dispose.
ComputerJy
Apr 9th, 2008, 01:51 AM
Here is the new code, I can't actually remember what is it that I've changed in your original code :P
import java.awt.Frame;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class JDialogNotification extends JDialog {
private static final long serialVersionUID = 9077763988467568226L;
private static int getTrayheight() {
return (Toolkit.getDefaultToolkit().getScreenSize().height - GraphicsEnvironment
.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
JDialogNotification dialog = new JDialogNotification(
new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
private void hideWindow() {
try { // / VBP - this line only
int _xAbove = getLocation().x;
int _yAbove = getLocation().y;
int _yBelow = getLocation().y + getHeight() + getTrayheight();
while (_yBelow > _yAbove) {
hideWindowMotion(this, _xAbove, _yAbove);
_yAbove += 1;
}
Thread.sleep(1000);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.sleep(1500);
dispose();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
private void hideWindowMotion(final JDialog mainDialog, final int x,
final int y) {
AbstractAction abAction = new AbstractAction() {
private static final long serialVersionUID = -1005676924292901160L;
public void actionPerformed(ActionEvent evnt) {
mainDialog.setLocation(x, y);
}
};
Timer motionTimer = new Timer(300, abAction);
motionTimer.setRepeats(false);
motionTimer.start();
}
public JDialogNotification(Frame owner, boolean modal) {
super(owner, modal);
initComponents();
setWindowLocation();
}
// Dialog motion
private void dialogMotion(final JDialog mainDialog, final int x, final int y) {
AbstractAction abAction = new AbstractAction() {
private static final long serialVersionUID = -1005676924292901160L;
public void actionPerformed(ActionEvent evnt) {
mainDialog.setLocation(x, y);
}
};
Timer motionTimer = new Timer(300, abAction);
motionTimer.setRepeats(false);
motionTimer.start();
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 138,
Short.MAX_VALUE));
layout.setVerticalGroup(layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 112,
Short.MAX_VALUE));
pack();
}
// Set the main window display location
public void setWindowLocation() {
try {
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int frameHeight = this.getHeight();
int frameWidth = this.getWidth();
int _xValue = screenWidth - frameWidth;
int _yValue = screenHeight - getTrayheight();
int _yEnd = screenHeight - frameHeight - getTrayheight();
// this.setLocation((screenWidth - frameWidth), (screenHeight -
// frameHeight - getTrayheight() - WINDOW_SPACE));
this.setLocation(_xValue, _yValue);
while (_yValue > _yEnd) {
dialogMotion(this, _xValue, _yValue);
_yValue -= 1;
}
Thread.sleep(1000);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Thread.sleep(1500);
hideWindow();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
} catch (InterruptedException ex) {
Logger.getLogger(JDialogNotification.class.getName()).log(
Level.SEVERE, null, ex);
}
}
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.