Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JFrame with invisible contents

Reply
Thread Tools

JFrame with invisible contents

 
 
Uli Kunkel
Guest
Posts: n/a
 
      12-02-2008
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.
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      12-02-2008
On Dec 2, 7:50*pm, Uli Kunkel <(E-Mail Removed)> wrote:
>..
> I would appreciate any suggestions.


Post an SSCCE*.

* <http://pscode.org/sscce.html>

--
Andrew Thompson
http://pscode.org/
 
Reply With Quote
 
 
 
 
Uli Kunkel
Guest
Posts: n/a
 
      12-02-2008
Peter Duniho wrote:
> On Tue, 02 Dec 2008 00:50:30 -0800, Uli Kunkel <(E-Mail Removed)> wrote:
>
>> 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.

>
> 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
}
}
----------------------------------------------------------------------------
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      12-02-2008
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Uli Kunkel schreef:
> Peter Duniho wrote:
>> On Tue, 02 Dec 2008 00:50:30 -0800, Uli Kunkel <(E-Mail Removed)>
>> wrote:
>>
>>> 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.

>>
>> 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.


> 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

iEYEARECAAYFAkk1BlsACgkQBGFP0CTku6M4JACeLgblDz3fpQ iSrwh3Poxqdvcn
2cgAn3iNt06DfjwsULj+kXMTkkxTaFiU
=5wdg
-----END PGP SIGNATURE-----
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      12-02-2008

Uli Kunkel wrote:
> Peter Duniho wrote:
>> On Tue, 02 Dec 2008 00:50:30 -0800, Uli Kunkel <(E-Mail Removed)>
>> wrote:
>>
>>> 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.

>>
>> 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);


new SwingWorker<Void><Void>() {
@Override public Void doInBackGround() {

> //some processing that takes a couple of seconds


}
}.execute();

> }
> }
> ----------------------------------------------------------------------------
>



--
RGB
 
Reply With Quote
 
Uli Kunkel
Guest
Posts: n/a
 
      12-02-2008
RedGrittyBrick wrote:
>
> Uli Kunkel wrote:
>> Peter Duniho wrote:
>>> On Tue, 02 Dec 2008 00:50:30 -0800, Uli Kunkel <(E-Mail Removed)>
>>> wrote:
>>>
>>>> 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.
>>>
>>> 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);

>
> new SwingWorker<Void><Void>() {
> @Override public Void doInBackGround() {
>
>> //some processing that takes a couple of seconds

>
> }
> }.execute();
>
>> }
>> }
>> ----------------------------------------------------------------------------
>>


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.
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      12-02-2008
Uli Kunkel wrote:
> 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.

--
Lew
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can I Get One JFrame To Appear Above Another JFrame clusardi2k@aol.com Java 2 08-27-2012 03:01 PM
Making 1 control invisible while showing another in the exact location of the invisible one Andy B ASP .Net 5 05-29-2008 03:08 AM
Invisible - Succinic acid : (AMBER ACID) Invisible krithika.143@gmail.com C++ 0 04-14-2008 06:59 PM
start a new JFrame from an existing one, and when old JFrame closes new one does not jakester Java 3 04-02-2007 11:13 PM
Re: Invisible JPanel within parent JFrame Roedy Green Java 0 07-30-2003 10:58 PM



Advertisments