Adding a window to a container

S

Sameer

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?
 
A

Andrew Thompson

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/c...0d3b7869650/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
 
T

Thomas Hawtin

Sameer said:
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]
 
S

Sameer

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?
 
A

Andrew Thompson

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

Roedy Green

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

Andrew Thompson

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'
 
A

Andrew Thompson

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

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top