> I have got a modal Jdialog (Jidalog extended class) that has a timer (wich
> is a private member of that class) started. The user should be able close
> tis dialog with the close-button or with the x in the caption bar. How can I
> make the dialg close?
1. Pressing X on the title bar closes the dialog.
2. To close the dialog via a button, assign ActionListener to the button,
and in the ActionListener's actionPerformed(ActionEvent e) method close the
dialog:
dialog.setVisible(false), or
dialog.hide()
> And how can I make sure the object including the timer is not existing
anymore?
3. Depending on the Timer class, you should be able to do (2) either in the
actionPerformed(ActionEvent e) method (java.swingx.Timer) or in TimerTask's
run() method (java.util.Timer).
HTH
Alex Molochnikov
Gestalt Corporation
Robert Ludewig - 16 Nov 2003 18:09 GMT
> 3. Depending on the Timer class, you should be able to do (2) either in the
> actionPerformed(ActionEvent e) method (java.swingx.Timer) or in TimerTask's
> run() method (java.util.Timer).
Just doing (2) will not make sure that I killed the dialog and the timer.
i.e. ht timer keeps on running and I could re-show the dialog ....
Here the code:
public class JProgressDialog extends JDialog implements ActionListener
{
private JLabel label;
private int counter;
private JProgressBar pgb;
private Timer t;
public JProgressDialog (Frame owner)
{
super(owner, true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new
BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
pgb = new JProgressBar();
label = new JLabel();
getContentPane().add(new JButton("cancel"));
getContentPane().add(label);
getContentPane().add(pgb);
t = new Timer(50,this);
t.start();
}
public void actionPerformed(ActionEvent e)
{
System.out.println(counter++);
if (counter < 100)
{
counter++;
label.setText(new Integer(counter).toString());
pgb.setValue(counter);
}
else
{
setVisible(false);
//dispose();
//t.stop();
}
}
}
ak - 16 Nov 2003 20:49 GMT
add WindowListener to your dialog and stop timer on windowClosed() event
> > 3. Depending on the Timer class, you should be able to do (2) either in
> the
[quoted text clipped - 45 lines]
> }
> }
Alex Molochnikov - 17 Nov 2003 01:16 GMT
See the answer inside.
> > 3. Depending on the Timer class, you should be able to do (2) either in
> the
[quoted text clipped - 40 lines]
> {
> setVisible(false);
t.stop();
> //dispose();
> //t.stop();
> }
> }
> }