I have a Problem:
I’m rendering a BufferedImage in a JFrame. Then i add a JButton to the same frame.
when i try to make the button transparent, the button becomes transparent, but disregarding its actual position, its always transparent like it is stuck in the top left corner of the frame.
I testet some different methods to make the button transparent, always with the same result.
any ideas?
thanks
public class TestPanel extends JPanel {
public TestPanel(){
JButton foo = new JButton("test");
foo.setBackground(new Color(0, 0, 0, 0));
foo.setBounds(20, 100, 300, 50);
this.add(foo);
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(ImageFactory.getImg(), 0, 0, null); //get a BufferedImage
g2.dispose();
}
}
I see several problems, even if I’m not sure on which of them cause your problem.I try to list them in order:
TestPaneldoesn’t specify aLayoutManager(I hope you are specifying it somewhere else in your code).You are extending a
JPanelwithout call super paintComponent method (don’t use paint). You should do this before anything else in your paintComponent method:remove the dispose method call. You must not destroy your graphic object.
EDIT:
this is a problem:
you are trying to explicitly set the bounds of your JButton. You shouldn’t do that. If you are using a LayoutManager it probably ignore this directive. If you are using a null layout this could be a problem too.