Adding a window to a container

Discussion in 'Java' started by Sameer, Sep 10, 2005.

  1. Sameer

    Sameer Guest

    Why I can't 'add a window to a container'?

    Please see the code-

    import java.awt.*;
    public final class SplashScreen extends Frame {

    private Image fImage;

    public SplashScreen(String aImageId) {
    if (aImageId == null || aImageId.trim().length() == 0) {
    throw new IllegalArgumentException(
    "Image Id does not have content.");
    }
    Toolkit tk = Toolkit.getDefaultToolkit();
    fImage = tk.createImage(aImageId);
    add(new SplashWindow(this, fImage), BorderLayout.CENTER);
    }

    private class SplashWindow extends Window {
    private Image fImage;

    SplashWindow(Frame aParent, Image aImage) {
    super(aParent);
    fImage = aImage;
    add(new ImageViewer(fImage));
    setVisible(true);
    }
    }
    }

    When it is executed using another Launcher programme, the stack trace is-
    java.lang.IllegalArgumentException: adding a window to a container

    at java.awt.Container.addImpl(Container.java:616)

    at java.awt.Container.add(Container.java:518)

    at SplashScreen.<init>(SplashScreen.java:18)

    at Launcher.main(Launcher.java:12)

    If it is not possible, what I have to do?
    How to modify this programme to create a SplashScreen class?


    --
    We have a hunger of the mind which asks for knowledge of all around us, and
    the more we gain, the more is our desire; the more we see, the more we are
    capable of seeing.
    -Maria Mitchell
     
    Sameer, Sep 10, 2005
    #1
    1. Advertising

  2. On Sat, 10 Sep 2005 17:14:11 +0530, Sameer wrote:

    > Why I can't 'add a window to a container'?


    Because a Window is a free floating thing, whereas a
    container contains ..components and panels with layouts.

    > How to modify this programme to create a SplashScreen class?


    Sorry, I could not be bothered looking at it, mostly because
    there are very good splash screen codes readily available.
    Here is one splash, both Roy Ratcliffe's very solid Splash,
    as well as my variant, suitable for applets and 1.1.
    <http://groups.google.com.au/group/comp.lang.java.gui/browse_frm/thread/a8f280d3b7869650/e4f38c661fbfb1ec#e4f38c661fbfb1ec>
    Have a look over those two examples* and see if you can
    make something workable.

    (* Unforutanely, Roy's links seem to be offline at the moment)

    [ Follow-Ups to this post set to comp.lang.java.gui only. ]

    HTH

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "I can't see it. But I know it enough to believe it"
    Hole 'Jennifer's Body'
     
    Andrew Thompson, Sep 10, 2005
    #2
    1. Advertising

  3. Sameer wrote:
    > Why I can't 'add a window to a container'?


    As Andrew Thompson said in another post, it doesn't make much sense. If
    you wanted MDI, then you could use JInternalFrame (which isn't-a
    Window). Just call setVisible(true) on it.

    > public final class SplashScreen extends Frame {


    You probably don't want a Frame as a splash screen, at least not
    decorated. Also it's a poor idea to extend classes you do not need to
    extend, which is probably causing some confusion in this case, as it
    often appears to.

    > add(new SplashWindow(this, fImage), BorderLayout.CENTER);
    > }
    >
    > private class SplashWindow extends Window {


    You are creating an inner class object, which by definition has a
    reference to the outer instance, and then passing in a second reference
    to the outer instance. This doesn't help clarity.

    Tom Hawtin

    [Followup-To: comp.lang.java.gui]
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Sep 10, 2005
    #3
  4. Sameer

    Sameer Guest

    Thanks for providing the link:
    http://www.randelshofer.ch/oop/javasplash/javasplash.html

    Well, I tried the classes given there. The files given on the site behave
    appropriately.
    But when applied to my application, the problem is that-
    The SplashScreen 'splashes' and go behind the main application and we need
    to click it to close it.
    This is not expected.
    Any guesses?

    --
    We have a hunger of the mind which asks for knowledge of all around us, and
    the more we gain, the more is our desire; the more we see, the more we are
    capable of seeing.
    -Maria Mitchell
     
    Sameer, Sep 10, 2005
    #4
  5. On Sat, 10 Sep 2005 19:11:37 +0530, Sameer wrote:

    > http://www.randelshofer.ch/oop/javasplash/javasplash.html

    ...
    > The SplashScreen 'splashes' and go behind the main application and we need
    > to click it to close it.


    AFAIR Roy's Splash used setAlwaysOnTop(), which is only
    available in 1.4/1.5?+. My adaptation took that into
    account, and fell back to a thread that drew the
    splash toFront() regularly.

    It might work better for you.

    Of course, the symptoms you claim, might be a broblem
    in the code you did not show. Another avenue is to
    show that code and we can take it from there.

    And Sameer, could I now ask you to please ..
    a) 'Reply-To' the post to which you are actually replying.
    Your 'thanks for the link' did not actually line up as
    a 'Reply' to my post, so it was a little confusing what you
    meant.

    Then, to add more still more understanding...
    b) Copy/paste - quote a little of what you are replying
    to, so the reader has a context (As I did above).
    Not all readers would be able to access the earlier
    posts in the thread, and quoting a little helps ensure
    everybody understands what is happening.

    [ Follow-Ups set to comp.lang.java.gui only. ]

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "I know what I want but I just don't know ..how to go about gettin' it."
    Jimi Hendrix 'Manic Depression'
     
    Andrew Thompson, Sep 10, 2005
    #5
  6. Sameer

    Roedy Green Guest

    On Sat, 10 Sep 2005 17:14:11 +0530, "Sameer"
    <> wrote or quoted :

    >Why I can't 'add a window to a container'?


    because the window already has a parent. The window can't play both
    roles of free-floating child and tightly bound inside.

    Here is a basic Splash class invoked with new Splash() early on in the
    load process.

    import java.awt.BorderLayout;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.net.URL;

    import javax.swing.JWindow;
    import javax.swing.Timer;

    /**
    * Splash screen shown during program load.
    */
    public class Splash extends JWindow
    {

    /**
    * constructor
    */
    Splash()
    {
    // get Image as a resource, from jar or from classpath
    URL url = this.getClass().getResource( Config.SPLASH_IMAGE );
    Image image = Toolkit.getDefaultToolkit().getImage( url );
    ImageViewer imageViewer = new ImageViewer( image );
    this.getContentPane().add( imageViewer, BorderLayout.CENTER );
    // allow user to close splash window with just a click
    this.addMouseListener(new MouseAdapter()
    {
    public void mousePressed( MouseEvent e
    )
    {
    setVisible( false );
    dispose();
    }
    });

    this.pack();
    this.setLocation( 150, 150 );
    this.setVisible( true );

    final ActionListener splashCloser = new ActionListener()
    {
    public void actionPerformed( ActionEvent evt )
    {
    // shutdown Splash
    setVisible( false );
    dispose();
    }
    };
    Timer splashTimer = new Timer( Config.SPLASH_TIME, splashCloser
    );
    splashTimer.setRepeats( false );
    splashTimer.start();
    }
    } // end class Splash.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Again taking new Java programming contracts.
     
    Roedy Green, Sep 10, 2005
    #6
  7. On Sun, 11 Sep 2005 02:02:28 GMT, Andrew Thompson wrote:

    > Keep doing what you're doing until I figure more,


    [This post is just an attempt to discover what is happening...]

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "We are about to attempt a crash landing. Please extinguish all
    cigarettes.."
    Laurie Anderson 'From The Air'
     
    Andrew Thompson, Sep 11, 2005
    #7
  8. On Sun, 11 Sep 2005 03:22:15 GMT, Andrew Thompson wrote:

    > On Sun, 11 Sep 2005 02:02:28 GMT, Andrew Thompson wrote:
    >
    >> Keep doing what you're doing until I figure more,

    >
    > [This post is just an attempt to discover what is happening...]


    Nope. Did not explain anything, since it threaded as I expected.

    [ I widened the X-post back to c.l.j.programmer, thinking that
    may cause it to not be threaded beneath the earlier posts, but
    it is. ]

    [ Follow-Ups (re) set to comp.lang.java.gui only. ]

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    "Finely tuned and well adjusted.."
    Do-Re-Mi 'Man Overboard'
     
    Andrew Thompson, Sep 11, 2005
    #8
    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. Vivi Orunitia
    Replies:
    11
    Views:
    4,502
    Martijn Lievaart
    Feb 4, 2004
  2. Maitre Bart
    Replies:
    2
    Views:
    536
    Maitre Bart
    Feb 11, 2004
  3. Steven T. Hatton
    Replies:
    4
    Views:
    3,936
    Rob Williscroft
    Dec 5, 2004
  4. Replies:
    4
    Views:
    815
    Daniel T.
    Feb 16, 2006
  5. wolverine
    Replies:
    2
    Views:
    462
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page