Page 1 of 1

Make a transparent swing UI components Rate Topic: ----- 1 Votes

#1 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Posted 09 July 2008 - 11:39 AM

In this tutorial I will explain how to create transparent swing UI components.

Letís start with simple example of how to make a transparent button:
class TransparentButton extends JButton {
	public TransparentButton(String text) { 
	    super(text);
	    setOpaque(false); 
	} 
	    
	public void paint(Graphics g) { 
	    Graphics2D g2 = (Graphics2D) g.create(); 
	    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
	    super.paint(g2); 
	    g2.dispose(); 
	} 
}


To use TransparentButton instead of JButton all what you want to modify is transparency level (I will show you how in next paragraph) and overwrite constructors you want as the above code.

All what you want to change transparency level is to change the 0.5f value in line g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
To make it clearer
0.0f means 100% transparent (you will not see it)
0.5f means 50% transparent
1.0f means 0% transparent (Not transparent at all)


Letís try this way now with popup menu:
class TransparentPopupMenu extends JPopupMenu {
	public TransparentPopupMenu() { 
		super();
		setOpaque(false); 
	} 
}

class TransparentMenuItem extends JMenuItem{
	public TransparentMenuItem(String text) { 
		super(text);
		setOpaque(false); 
	} 

	public void paint(Graphics g) { 
		Graphics2D g2 = (Graphics2D) g.create(); 
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f)); 
		super.paint(g2); 
		g2.dispose(); 
	} 
}


Note: You can get rid of TransparentPopupMenu class but in this case you have to remember to call setOpaque(false); after initialize JPopupMenu object

And you can try this way with other components.

Is This A Good Question/Topic? 2
  • +

Replies To: Make a transparent swing UI components

#2 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Posted 06 August 2008 - 03:30 PM

A picture that shows the effects of this code would be nice. If you wouldn't mind, it would be helpful.
Was This Post Helpful? 0
  • +
  • -

#3 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Posted 06 August 2008 - 05:26 PM

View Postabgorn, on 6 Aug, 2008 - 03:30 PM, said:

A picture that shows the effects of this code would be nice. If you wouldn't mind, it would be helpful.

Attached Image
Attached Image
Was This Post Helpful? 0
  • +
  • -

#4 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Posted 29 August 2008 - 02:59 AM

Thanks, I hope to use it in my own work. :D
Was This Post Helpful? 0
  • +
  • -

#5 gumuruh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-December 09

Posted 04 March 2011 - 03:08 AM

View Postabgorn, on 29 August 2008 - 02:59 AM, said:

Thanks, I hope to use it in my own work. :D


wait, wait....
How could we apply this in JFRame?
Since JFrame doesn't have a setOpaque(true) method?
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Posted 04 March 2011 - 03:34 AM

You can't make JFrame's not-opaque. They're not a pure vanilla Java component. Look at my library if you want to force it to be:
http://www.dreaminco...-swing-library/
Was This Post Helpful? 0
  • +
  • -

#7 reizark94  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-February 12

Posted 09 February 2012 - 02:57 PM

View Postlordms12, on 06 August 2008 - 05:26 PM, said:

View Postabgorn, on 6 Aug, 2008 - 03:30 PM, said:

A picture that shows the effects of this code would be nice. If you wouldn't mind, it would be helpful.

Attachment attachment
Attachment attachment

how can I use this code in my program, if I have an frame???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1