Swing Windowless Component?

Go To StackoverFlow.com

0

I want to show a spinning progress wheel thing (like this) when something's processing to show the user something's happening. Is there any way I can do this without popping up a whole window for it? I'm using swing. Thanks.

2012-04-04 20:24
by kentcdodds


3

Here are two stripped-down instructional examples of how to pop-display an image on the screen. Adjust image source, position, size, exception handling etc. as necessary.

Example 1 of 2, Semi-transparent using JLabel:

public static void main(String args[]) throws Exception {
JWindow jWindow = new JWindow() {
final Icon icon = new ImageIcon(<yourImage>);  // Okay to be animated
{
    setOpacity(.642f);
    setLocation(0,0);
    setSize(icon.getIconWidth(), icon.getIconHeight());
    add(new JLabel(icon));
    pack();
}
};
jWindow.setVisible(true);
Thread.sleep(3000);
jWindow.setVisible(false);
}

Example 2 of 2, Transparent using paint:

public static void main(String args[]) throws Exception {
JWindow jWindow = new JWindow() {
final Image image = ImageIO.read(<yourImage>);  // Static image only
{
    setLocation(0,0);
    setSize(image.getWidth(), image.getHeight());
}
public void paint(Graphics g) {
    g.drawImage(image, 0, 0, null);
}
};
jWindow.setVisible(true);
Thread.sleep(3000);
jWindow.setVisible(false);
}
2012-04-04 22:13
by Java42
I really like this method! I want to use the first one you mentioned, but when I give it an animated gif it just shows up as a grey box with nothing in it. When I give it a regular png it works fine. Also, how do I change the background color? It's currently grey, I'd like it to be white. I tried setBackground(Color.white) but that didn't work. Thanks for the help, it'll work great when I get it working - kentcdodds 2012-04-05 03:36
Animation is working on my Win7/Eclipse/JDK17 system. There is no background color to set since the image itself determines the color - Java42 2012-04-06 05:21


4

Assuming that your application is using a Swing container of some sort, I would recommend setting the animated gif as the icon of a JLabel and place this onto the GlassPane of the container. And then toggle this pane whenever necessary.

Here is a partially relevant implementation (note the blocking of input events).

2012-04-04 20:27
by mre
I'll look into it! Great! I'd never heard of a GlassPane before. Looks awesome - kentcdodds 2012-04-04 20:34
Ads