Article View: borland.public.jbuilder.java.swing
Article #11972JDialog behaves differently according to where I call it
From: George Tryfon
Date: Tue, 23 Mar 1999 00:00
Date: Tue, 23 Mar 1999 00:00
170 lines
4708 bytes
4708 bytes
This dialog behaves different according to where I call it.
It is a kind of progress indicator dialog that has a cancel button
The dialog I am referring to is the DlgWait
If I call the Frame.go from the main, then everything works fine.
If I call the Frame.go from the actionPerformed, the DlgWait is displayed,
but it does not allow me to click the Cancel button to stop the operation
It seems to me that somehow, because the process is bizy does not handle the
"button pressed" event.
Does anybody has an explanation for the reason that this happens ?
Does enybody has a workaround on this problem ?
Here is the code:
-----------------
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
// \ / /\ /\
// (o)(o) <|> <|>
// [ \/ ] =0= //
// \ / / \||
// | |//
// [ ][ ]
//
public class Frame extends JFrame
{
Frame m_Me;
JButton m_Btn;
Frame ()
{
super();
m_Me = this;
m_Btn = new JButton ("go");
this.getContentPane().add (m_Btn);
m_Btn.addActionListener (new ActionListener (){
public void actionPerformed (ActionEvent ev)
{
m_Me.go (); //HERE DOES NOT WORK
}
});
}
void go ()
{
int max = 1000;
DlgWait pr = new DlgWait (this, "waiting...");
pr.Show ("waiting...");
//pr.setMinimum (0);
pr.setMaximum (max);
for (int i=0; i<max; i++)
{
pr.setProgress (i);
if (pr.isCanceled ())
{
JOptionPane.showMessageDialog(null, "Message", "canceled",
JOptionPane.INFORMATION_MESSAGE);
break;
}
}
JOptionPane.showMessageDialog(null, "Message", "terminated normally",
JOptionPane.INFORMATION_MESSAGE);
System.exit (0);
}
public static void main(String args[])
{
Frame f = new Frame ();
f.pack ();
f.setVisible (true);
f.go (); //HERE WORKS OK
}
}
/////////////////////////////////////
public class DlgWait extends JDialog
{
DlgWait m_Me;
JLabel m_Label;
JProgressBar m_Prog;
JButton m_Cancel;
boolean m_bCanceled;
DlgWait (Frame f, String fName)
{
super (f, fName, false);
m_Me = this;
this.getContentPane().setLayout (new GridLayout (3,1));
m_Label = new JLabel ();
m_Label.setPreferredSize (new Dimension(100, 20));
this.getContentPane().add (m_Label);
m_Prog = new JProgressBar ();
this.getContentPane().add (m_Prog);
m_Cancel = new JButton ("Cancel");
this.getContentPane().add (m_Cancel);
m_Cancel.addActionListener (new ActionListener (){
public void actionPerformed (ActionEvent ev)
{
m_Me.Hide ();
}
});
m_bCanceled = false;
pack ();
}
void Show (String s)
{
CenterMe ();
this.setVisible (true);
m_Label.setText (s);
for (int ii=0; ii<30; ii++)
ShowAll ();
}
void Hide ()
m_bCanceled = true;
this.setVisible (false);
}
void ShowAll ()
{
m_Label.paintImmediately (m_Label.getBounds());
m_Prog.paintImmediately (m_Label.getBounds());
m_Cancel.paintImmediately (m_Label.getBounds());
}
void CenterMe ()
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = getSize();
int x = screenSize.width / 2 - size.width / 2;
int y = screenSize.height / 2 - size.height / 2;
setBounds (x, y, size.width, size.height);
}
void setNote (String s)
{
m_Label.setText (s);
m_Label.paintImmediately (m_Label.getBounds());
}
void setMinimum (int i)
{
m_Prog.setMinimum (i);
}
void setMaximum (int i)
{
m_Prog.setMaximum (i);
}
void setProgress (int i)
{
m_Prog.setValue (i);
m_Prog.paintImmediately (m_Label.getBounds());
}
boolean isCanceled ()
{
return m_bCanceled;
}
}
Technical Details
Message-ID:
<7d7f8s$k4u20@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,5d75d4f2f4d6bd51,start X-Google-Attributes: gid111d45,public From: "George Tryfon" <george@compucon.gr> Subject: JDialog behaves differently according to where I call it Date: 1999/03/23 Message-ID: <7d7f8s$k4u20@forums.borland.com> X-Deja-AN: 457886394 Organization: Another Netscape Collabra Server User X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Newsgroups: borland.public.jbuilder.java.swing