JFrame with invisible contents

Discussion in 'Java' started by Uli Kunkel, Dec 2, 2008.

  1. Uli Kunkel

    Uli Kunkel Guest

    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.
    Uli Kunkel, Dec 2, 2008
    #1
    1. Advertising

  2. On Dec 2, 7:50 pm, Uli Kunkel <> wrote:
    >..
    > I would appreciate any suggestions.


    Post an SSCCE*.

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

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Dec 2, 2008
    #2
    1. Advertising

  3. Uli Kunkel

    Uli Kunkel Guest

    Peter Duniho wrote:
    > On Tue, 02 Dec 2008 00:50:30 -0800, Uli Kunkel <> 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
    }
    }
    ----------------------------------------------------------------------------
    Uli Kunkel, Dec 2, 2008
    #3
  4. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

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

    iEYEARECAAYFAkk1BlsACgkQBGFP0CTku6M4JACeLgblDz3fpQiSrwh3Poxqdvcn
    2cgAn3iNt06DfjwsULj+kXMTkkxTaFiU
    =5wdg
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Dec 2, 2008
    #4
  5. Uli Kunkel wrote:
    > Peter Duniho wrote:
    >> On Tue, 02 Dec 2008 00:50:30 -0800, Uli Kunkel <>
    >> 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
    RedGrittyBrick, Dec 2, 2008
    #5
  6. Uli Kunkel

    Uli Kunkel Guest

    RedGrittyBrick wrote:
    >
    > Uli Kunkel wrote:
    >> Peter Duniho wrote:
    >>> On Tue, 02 Dec 2008 00:50:30 -0800, Uli Kunkel <>
    >>> 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.
    Uli Kunkel, Dec 2, 2008
    #6
  7. Uli Kunkel

    Lew Guest

    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
    Lew, Dec 2, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    3,041
    Roedy Green
    Jul 30, 2003
  2. Don Adams
    Replies:
    1
    Views:
    580
    Martin Honnen
    Mar 5, 2004
  3. jakester
    Replies:
    3
    Views:
    1,802
    jakester
    Apr 3, 2007
  4. Andy B
    Replies:
    5
    Views:
    573
    Andy B
    May 29, 2008
  5. Replies:
    2
    Views:
    400
    markspace
    Aug 27, 2012
Loading...

Share This Page