Extends, problem

N

Niall

Hey this is more than likely a very common question, but a question all the
same, I'm creating a splash screen, and have it working ok, As in it
disappears when I click on it but what I want to do is have a timer on it,
so I though I would use thread but....... the embarrassing question.
How can extend JWindow and Threads at the same time?

Thanks in advance.
Niall
 
C

Christophe Vanfleteren

Niall said:
Hey this is more than likely a very common question, but a question all
the same, I'm creating a splash screen, and have it working ok, As in it
disappears when I click on it but what I want to do is have a timer on it,
so I though I would use thread but....... the embarrassing question.
How can extend JWindow and Threads at the same time?

As you noticed, you can't inherit from more than 1 class in Java. Interfaces
are used to simulate multiple inheritance instead.

So you could implement Runnable.

But a better idea would be to create an anonymous inner class to do the job:

Thread t = new Thread() {
public void run() {
//do whatever you need to
}
};
t.start();

You might also want to look at java.util.Timer or javax.swing.Timer.
 
N

Niall

Thats what I tried but it's looking the constructor Thread.
Thats when I dont extend it.
 
C

Christophe Vanfleteren

Niall said:
Thats what I tried but it's looking the constructor Thread.
Thats when I dont extend it.

I don't quite follow here.
Could you show the code? Or at the very least the exception you're getting?
 
N

Niall

public class ConferenceSplash extends JWindow implements MouseListener
{

private Thread thread;

public ConferenceSplash(){
thread = new Thread(this);
}

where I am extending JWindow, I know I need to Extend Thread enable to use
thread, butI dont know how to enable both of them.
 
C

Christophe Vanfleteren

Niall said:
public class ConferenceSplash extends JWindow implements MouseListener
{

private Thread thread;

public ConferenceSplash(){
thread = new Thread(this);
}

where I am extending JWindow, I know I need to Extend Thread enable to use
thread, butI dont know how to enable both of them.

Did you read my previous answer? You could also implement Runnable, then
this would work, as the Thread constructor expects a Runnable object.

But the other code I posted should also work, and is somewhat better IMHO.
 
N

Niall

Sorry I forgot to implemt runnible, but it is going very well now thank
you, Im using thread.sleep(int) for the amout of display time my splash
need's. Thanks very much for your assistance.

Niall Mulhare
 
C

Corey Brown

Niall said:
Sorry I forgot to implemt runnible, but it is going very well now thank
you, Im using thread.sleep(int) for the amout of display time my splash
need's. Thanks very much for your assistance.

You may want to reconsider what you're doing with Thread.sleep().
The problem is that while you have the main thread sleeping, the window
won't refresh itself if overlayed. Probably not what you want.

--Corey
 
R

Roedy Green

Hey this is more than likely a very common question, but a question all the
same, I'm creating a splash screen, and have it working ok, As in it
disappears when I click on it but what I want to do is have a timer on it,
so I though I would use thread but....... the embarrassing question.
How can extend JWindow and Threads at the same time?

Here is a timed splash.

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

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

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

/**
* constructor
*/
Splash()
{
super();

this.getContentPane().setLayout( null );
this.setLocation( 150, 150 );
// size of frame around image
this.setSize( 330, 250 );
this.setBackground ( Config.MATTE_COLOR );
this.setVisible( true );
}

/**
* second stage of initialisation.
* Can't get images until peer created.
*/
public void addNotify()
{
super.addNotify();
URL url = Splash.class.getResource( "images/splash.gif" );

Image splash = this.getToolkit().getImage( url );

JpgViewer j = new JpgViewer( 330, 250, JpgViewer.DOUBLE_BUFFERED
);
j.setBackground ( Config.MATTE_COLOR );
j.setImage( splash );
j.setLocation ( 0, 0 );
this.getContentPane().add ( j );
// allow user to close splash window with just a click
MouseListener closer = new MouseAdapter()
{
/**
* Invoked when the mouse has been clicked on a component.
*/
public void mouseClicked( MouseEvent e )
{
Splash.this.setVisible( false );
Splash.this.dispose();
}
};
this.addMouseListener( closer );

// don't start timing until almost visible.
Timer splashTimer = new Timer( Config.SPLASH_TIME, this );
splashTimer.setRepeats( false );
splashTimer.start();
}

/**
* invoked whenever Timer expires
*
* @param e event not used.
*/
public void actionPerformed( ActionEvent e )
{
setVisible( false );
dispose();
}

/**
* test harness
*
* @param args not used
*/
public static void main ( String[] args )
{
if ( Config.DEBUG )
{
new Splash();
}
}
} // end class Splash.

Instead of JPGViewer you could use a JLabel with IconImage or an
ImageViewer see http://mindprod.com/products.html#BUS
 
R

Roedy Green

Thats what I tried but it's looking the constructor Thread.
Thats when I dont extend it.

You are not really creating a new kind of Thread. So you don't really
want to extend Thread. You simply want to have a method in your class
that can be invoked on separate thread. So you want to implement
Runnable. Better still use the simpler Timer method I showed you.

The advantage of the Timer method is I suspect if you have multiple
Timers it may be smart enough to manage them all with one thread.

Every Thread is AT LEAST 1 MB overhead. You don't want to create new
ones unless you really have to.
 
R

Roedy Green

Sorry I forgot to implemt runnible, but it is going very well now thank
you, Im using thread.sleep(int) for the amout of display time my splash
need's. Thanks very much for your assistance.

You don't want to sleep. That will tie up the thread so no other work
can get done. You want a TIMER that lets you continue doing useful
work, but will shut down the splash later.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top