Subscribe to Dogstopper's Code Mania        RSS Feed
-----

Transparent Swing Components

Icon 1 Comments
Transparent Components in Swing are not normally thought of, which leads to issues when adding components unsuspectingly to a component that already has a background and you don't see the background color. That's because most likely, one is adding a non-transparent component to it, which "covers up" the color. In this tutorial, I will demonstrate how to make many components "Transparent". The only exception is JFrame, which requires several hacks to make itself transparent (later entry). So, let's begin.

"Normal" Components
Well, let's start with adding a JPanel to a JFrame with a blue background. Here's how one would normally think to do it:
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class TransFrame extends JFrame {

	private static final long serialVersionUID = -352211755854169871L;

	public TransFrame() {

		// Set up the JFrame and make the background Blue
		super("Transparent Panel Test");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		// Remember, you cannot directly set the JFrame's background...must do the content pane
                getContentPane().setBackground(Color.BLUE);

		// Now add a panel to it.
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(200, 100));
		add(panel);

		pack();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new TransFrame();
	}
}



However, when you run it, you will see no blue! That's because a JPanel, by default is not transparent and so it has its own color. However, there are actually several quick fixes to this, but personally, I think the best is to make it transparent (not-opaque). Fortunately, there is a method called setOpaque(). Merely pass "false" into there and it will be transparent.

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class TransFrame extends JFrame {

	private static final long serialVersionUID = -352211755854169871L;

	public TransFrame() {

		// Set up the JFrame and make the background Blue
		super("Transparent Panel Test");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		// You can only paint the content pane, not any
		// other part of a JPanel
		getContentPane().setBackground(Color.BLUE);

		// Now add a panel to it.
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(200, 100));
		panel.setOpaque(false);
		add(panel);

		pack();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new TransFrame();
	}
}



Like JPanel, most components can be made transparent simply by calling setOpaque(). The weirdo is JLabel, which is actually non-opaque by default.

The JLabel
Unlike several components, JLabels are actually transparent by default as seen here:
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class TransFrame extends JFrame {

	private static final long serialVersionUID = -352211755854169871L;

	public TransFrame() {

		// Set up the JFrame and make the background Blue
		super("Transparent Component Test");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		
		// You can only paint the content pane, not any
		// other part of a JPanel
		getContentPane().setBackground(Color.BLUE);

		// Now add a panel to it.
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(200, 100));
		panel.setOpaque(false);
		add(panel);
		
		JLabel lab = new JLabel("TRANSPARENT BY DEFAULT");
		panel.add(lab);
		
		JLabel transLab = new JLabel("NON-TRANSPARENT");
		transLab.setOpaque(true);
		panel.add(transLab);

		pack();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new TransFrame();
	}
}



The JButton
Several components, like this one, are unaffected by setOpaque(). In particular, JButton has a method called setContentAreaFilled(), which you then set to false.
		
		JButton but = new JButton("THIS IS A BUTTON");
		but.setContentAreaFilled(false);
		panel.add(but);



The Conclusion
Most of the time, one will use the setOpaque() method, but if that component does not support that method, you will have to look around a little for the right method. Try things and explore. You will honestly be surprised once you explore around Swing enough. As for making JFrames transparent...that's another lesson for another day.

1 Comments On This Entry

Page 1 of 1

Mercurial 

27 August 2010 - 03:38 AM
Whenever transparent components were mentioned, I'd always think about playing with BufferedImage's alpha. This is interesting.
0
Page 1 of 1

December 2018

S M T W T F S
      1
2345678
9101112131415
16 171819202122
23242526272829
3031     

Recent Entries

Search My Blog

Recent Comments

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)