Recent top five:
Java.next -- Four languages that represent the future of Java
Blogger Stuart Halloway has begun a series of posts on trends that point to the future of the Java platform. In his first
post, he compares Clojure, Groovy, JRuby, and Scala -- four wildly different languages that nonetheless all play together
in the JRE. Find out what unites these languages and what they can tell us about the future of Java-based development ...
| Enterprise AJAX - Transcend the Hype |
| Memory Analysis in Eclipse |
| Oracle Compatibility Developer's Guide |
| Memory Analysis in Eclipse |
Wouldn't you like to be able to do all those things in Java? By using Swing with threads, you can!
Here is a first swing (no pun intended) at a class for a splash screen created for use in an application:
class SplashWindow1 extends JWindow
{
public SplashWindow1(String filename, Frame f)
{
super(f);
JLabel l = new JLabel(new ImageIcon(filename));
getContentPane().add(l, BorderLayout.CENTER);
pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.width/2 - (labelSize.width/2),
screenSize.height/2 - (labelSize.height/2));
setVisible(true);
screenSize = null;
labelSize = null;
}
}
The SplashWindow1 class extends Swing's JWindow. JWindow is a heavyweight container. It also has none of the normal items that appear in other windows, such as a title bar, window
management buttons, or even a visible frame edge. Therefore, JWindow is perfect for a splash screen. The code above assumes an image file is located in the current directory. Once the image
is loaded by way of the ImageIcon, the image is placed in the center of the JWindow. The JWindow is packed to let Swing resize the window correctly, and then it is moved to the center of the screen and set visible. You
can find a similar version of that code in the reference material in Resources.
If you were to actually run the above code, you would unfortunately have a nicely centered splash screen that will not close! To make it close, you must add code:
class SplashWindow2 extends JWindow
{
public SplashWindow2(String filename, Frame f)
{
super(f);
JLabel l = new JLabel(new ImageIcon(filename));
getContentPane().add(l, BorderLayout.CENTER);
pack();
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.width/2 - (labelSize.width/2),
screenSize.height/2 - (labelSize.height/2));
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
setVisible(false);
dispose();
}
});
setVisible(true);
}
}
The only difference in that version of the SplashWindow class is that there is now an anonymous MouseListener installed on the JWindow. That will allow the user to click on the splash screen to make it disappear.
At that point, you will have a nice splash screen that can be removed but will not disappear on its own. You will then have to add code to remove the splash screen after a certain amount of time. Then you should be thinking threads. And if you've worked with Swing at all, you know that making threaded calls can be tricky at best. For reasons why and a more in-depth explanation of threads and Swing, see Resources.