java - How can I make JRadioButton transparent in particular case? -
i have couple of jradiobutton: rb1, rb2; contained in transparent jpanel p1, , p1 contained in colorful panel, named mainpanel. want make these jradiobutton transparent , here do:
in mainpanel: mainpanel.setbackground(color.red);
in p1: p1.setbackground(new color(0,0,0,0));
and in rb1 , rb2:
rb1.setopaque(false); rb1.setcontentareafilled(false); rb1.setborderpainted(false); rb2.setopaque(false); rb2.setcontentareafilled(false); rb2.setborderpainted(false); it's ok if rb1 , rb2 contained in mainpanel or if p1 isn't transparent jpanel, in case, outcome not expected: 
how can resolve problem? in advance!
the weird painting artifacts you're seeing caused this:
p1.setbackground(new color(0,0,0,0)); with parent container not notified clear it's background , repaint. if want panel transparent, use setopaque(false) instead. need call method on radio buttons , nothing else.
setopaque notify parent repaint, if want semi-transparent panel, have override paintcomponent , call super.paintcomponent(graphics) manually.
import java.awt.color; import java.awt.eventqueue; import javax.swing.buttongroup; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jradiobutton; public class example { public void createandshowgui() { jradiobutton encryptbutton = new jradiobutton("encrypt"); encryptbutton.setopaque(false); jradiobutton decryptbutton = new jradiobutton("decrypt"); decryptbutton.setopaque(false); buttongroup group = new buttongroup(); group.add(encryptbutton); group.add(decryptbutton); jpanel subpanel = new jpanel(); subpanel.setopaque(false); subpanel.add(encryptbutton); subpanel.add(decryptbutton); jpanel mainpanel = new jpanel(); mainpanel.setbackground(color.cyan); mainpanel.add(subpanel); jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setcontentpane(mainpanel); frame.pack(); frame.setvisible(true); } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { new example().createandshowgui(); } }); } } 
Comments
Post a Comment