Search code examples
javaswingprogressjcomponent

Swing Windowless Component?


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.


Solution

  • 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);
    }