JFrame with invisible contents

U

Uli Kunkel

I have an applet in which a JFrame with a progress bar is displayed
after a button is clicked.
The JFrame is created in the init() and it is invisible.
In the desired event I set the frame visibility to true.
The frame is shown but it's contents is not visible until the whole
button event is processed.

I would appreciate any suggestions.
 
U

Uli Kunkel

Peter said:
Without a concise-but-complete code sample demonstrating the problem,
it's impossible to say. But, it sounds as though you are executing the
work in response to the button press from the Action event handler. If
so, then you're blocking the event-dispatch thread, preventing your
JFrame from being able to redraw its contents, or any other UI events
for that matter.

If that turns out to be the case, then you should be moving your lengthy
processing to a different thread, and let the EDT do its thing.

If that's not the case, then you should post a more specific, more
complete question.

Pete

In the init method the frame is defined:

-----------------------------------------------------------
jpb = new JProgressBar(0, 100);
jpb.setPreferredSize(new Dimension(170, 20));

jf = new JFrame("Title");
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
jf.setPreferredSize(new Dimension(300, 90));
jf.setLocation(190, 90);
jf.getContentPane().add(new JLabel("Please wait..."));
jf.getContentPane().add(jpb);

jf.pack();
jf.setVisible(false);
-----------------------------------------------------------

Then it is set visible in the action performed of the button:

---------------------------------------------------------------------------
private void myButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(!jf.isVisible())
{
jf.setVisible(true);
//some processing that takes a couple of seconds
}
}
----------------------------------------------------------------------------
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Uli Kunkel schreef:
private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(!jf.isVisible())
{
jf.setVisible(true);
//some processing that takes a couple of seconds

This is exactly the problem Peter pointed out: you do ‘some processing
that takes a couple of seconds’ on the Event Dispatch Thread, and thus
the GUI has no chance to update itself, since that happens on the EDT.
If you use Java 6, look at the StringWorker class, otherwise, write one
yourself, there are enough examples on the internet.

Oh, and do read the link Andrew pointed you to, it’s called SSCCE.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iEYEARECAAYFAkk1BlsACgkQBGFP0CTku6M4JACeLgblDz3fpQiSrwh3Poxqdvcn
2cgAn3iNt06DfjwsULj+kXMTkkxTaFiU
=5wdg
-----END PGP SIGNATURE-----
 
R

RedGrittyBrick

Uli said:
In the init method the frame is defined:

-----------------------------------------------------------
jpb = new JProgressBar(0, 100);
jpb.setPreferredSize(new Dimension(170, 20));

jf = new JFrame("Title");
jf.setLayout(new FlowLayout(FlowLayout.CENTER));
jf.setPreferredSize(new Dimension(300, 90));
jf.setLocation(190, 90);
jf.getContentPane().add(new JLabel("Please wait..."));
jf.getContentPane().add(jpb);

jf.pack();
jf.setVisible(false);
-----------------------------------------------------------

Then it is set visible in the action performed of the button:

---------------------------------------------------------------------------
private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(!jf.isVisible())
{
jf.setVisible(true);

//some processing that takes a couple of seconds

}
}.execute();
 
U

Uli Kunkel

Thank you for the answer.
I created a new thread for the processing and now it works ok.
Before I tried to put the painting of the frame in a separate thread but
that doesn't work.
 
L

Lew

Uli said:
Before I tried to put the painting of the frame in a separate thread but
that doesn't work.

That's because GUI work *must* happen on the EDT, only.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top