Java Tips, Views and Techniques

Java-Dev ML; Java Performance on Mac OS X

In the last few days there has been some heated exchanges over performance of Java on Mac OS X within the Java-Dev mailing list hosted by Apple. The list is at http://lists.apple.com/mailman/listinfo/java-dev if you want to check it out. There has been some remarks and rebuttals made about the creation of Java and its alleged performance problems. James Gosling also provides his input on some of the comments being made. Pretty interesting.

Java Blogs

Just signed up my blog to Java Blogs at http://www.javablogs.com/ created by Atlassian and not hosted by Sun Microsystems. It's great to see a community being developed for Java using blogs. It seems to generate a lot of interest as I am getting a lot of hits from the site. Nonetheless, I can now put the Java Blogs logo on my site.

Java Blogs Logo

More on JavaOne 2003

This post is kinda late but I'm back from JavaOne 2003. It was pretty good but nothing like the first year I went in the middle of the dot com craze. The hype and endless piles of money for free giveaways was a sight to behold. After four years I've come to the conclusion that if you are a vendor trying to sell your worthless goods at JavaOne, there are two basic things you need to do.

The first is to have some decent eye-candy at your booth. Don't bother trying to get anybody who knows anything because most of the people coming up to the booths don't know anything anyway. Just get some gimmick with some hot chicks and you're on your way.

Second, don't bother with any giveaway except for a t-shirt. Although, a big giveaway through a drawing does attract a lot of attention too. Nonetheless, the t-shirt lasts forever because those who get free t-shirts wear them around if they think they are cool. Sink plenty of money and time into making the best t-shirt and just give it away at the booth. Have your girls handing them out for the guy's information. Just slide that card and get the info you need to solicit to your heart's content.

Now you have the two things you need: an audience to solicit and an audience that will promote your brand by wearing your cool t-shirt wherever they go. If you get good enough at this, there will be a loyal group that will start to form and will look forward to getting your t-shirts every year. This will further your network.

JavaOne 2003

It's that time of year and the JavaOne conference is here. I've already registered and will be attending for my fourth year. It should be interesting. It's also the second year that the JBoss Group will be there with their own brand of JbossTwo conference to be held at the Sony Metreon right down the street from the Moscone Center. Here are the links for more info:

http://java.sun.com/javaone/

http://www.jboss.org/

Java theory and practice: Urban performance legends

Great article on performance for Java and some the myths surrounding it. It's a great read. Check it out at http://www-106.ibm.com/developerworks/java/library/j-jtp04223.html?ca=dgr-lnxw01JavaUrbanLegends

Input Validation with Filters

http://www.fawcette.com/javapro/2002_08/online/servlets_08_13_02/

Develop Faster With Tag Libraries

http://www.fawcette.com/javapro/2003_01/online/servletsjsp_bkurniawan_01_09_03/

Create thread-safe splash screens using Swing

Here's a pretty cool java tip to use for creating splash screens in swing. Check it out.

http://www.javaworld.com/javaworld/javatips/jw-javatip104.html

Summary
Creating splash screens in Java by using AWT and Swing is well documented in the current literature. However, the existing solutions are not fully featured in that documentation and do not explain all the intricacies of threading in Swing. This tip presents a splash-screen component on which the user can click to make it disappear. In addition, the component will also have a timer controlling the splash screen so that it will automatically disappear after a set period of time. (1,000 words)

Almost all modern applications have a splash screen. Using a splash screen is a way to advertise your product. It is also used to indicate to the user that something is happening in your application during long startup times. Current literature explains how to create a splash screen but does not show how to integrate one into your application (see books by David Geary). Users can quickly remove some splash screens by simply clicking anywhere on them. Some splash screens stay visible only until the application is loaded. Other splash screens are visible even after the user has started the application.

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.


class SplashWindow3 extends JWindow
{
public SplashWindow3(String filename, Frame f, int waitTime)
{
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();
}
});
final int pause = waitTime;
final Runnable closerRunner = new Runnable()
{
public void run()
{
setVisible(false);
dispose();
}
};
Runnable waitRunner = new Runnable()
{
public void run()
{
try
{
Thread.sleep(pause);
SwingUtilities.invokeAndWait(closerRunner);
}
catch(Exception e)
{
e.printStackTrace();
// can catch InvocationTargetException
// can catch InterruptedException
}
}
};
setVisible(true);
Thread splashThread = new Thread(waitRunner, "SplashThread");
splashThread.start();
}
}

The general idea here is to first create a Thread object that will pause for a specific amount of time. In the above code, the thread will pause for four seconds. When that thread wakes up, it will close the splash screen. Since Swing is not thread-safe, you should not affect the state of any UI component unless the code is being executed on the event-dispatching thread. The event-dispatching thread is the thread that handles drawing and event handling in Swing.

To get around that limitation, Swing designers gave the programmer the ability to add runnable objects to the UI event queue in a safe manner. In this case, you are going to use the runnable object's closeRunner to do the dirty work. You pass the runnable object to the static method SwingUtilities.invokeAndWait(). Then SwingUtilities.invokeAndWait() will execute all pending UI activity and execute the run method on the runnable object closeRunner, which is passed to the method. By using a separate thread to handle the splash screen's closing, the application that is displayed behind the splash screen is visible and responsive during the entire operation.

If you want a splash screen that is always visible and that the user cannot remove, you must remove the code that hides the splash screen. If you want a splash screen that the user must close manually, you can call the setVisible(false) and dispose() methods on the SplashWindow3 object just like any other JWindow.

Conclusion
By using the SwingUtilities.invokeAndWait() method, you can safely create a multithreaded Swing splash screen. A user can click on the splash screen to remove it, or the splash screen will disappear on its own after a set amount of time. The threading model that Swing supports will allow the application to remain responsive and usable behind the splash screen.


<< Previous 10 Articles  31 - 38 of 38 articles  

On This Site

  • About this site
  • Main Page
  • Most Recent Comments
  • Complete Article List
  • Sponsors

Search This Site


Syndicate this blog site

Powered by BlogEasy


Free Blog Hosting