I am confused regarding the display of components in a JPanel.
Suppose if I create a custom JPanel with translucency of 0.8f as follows :-
JPanel panel=new JPanel(){
@Override
public void paint(Graphics g)
{
super.paint(g);
BufferedImage img=(BufferedImage)createImage(getWidth(),getHeight());
Graphics2D g2=(Graphics2D) g.create();
g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
g2.drawImage(img,0,0,null);
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(300,300);
}
};
Now I set it as the contentPane of the frame.
frame.setContentPane(panel);
Now I add some buttons to it.
frame.add(new JButton("Click Here"));
frame.add(new JButton("Click Here"));
frame.add(new JButton("Click Here"));
frame.add(new JButton("Click Here"));
1)Then in the output why I get translucent buttons?As JPanel is single layered and I painted the translucent image first when I overrided its paint
method and then buttons were added,the buttons must not be translucent as they should come over it.
2)Also out of those 4 buttons only 2 are translucent .Why is there such partiality?
3)If I also add a table before adding these 4 buttons then everything becomes translucent.Why?
Object[] names = new Object[] {
"Title", "Artist", "Album"
};
String[][] data = new String[][] {
{ "Los Angeles", "Sugarcult", "Lights Out" },
{ "Do It Alone", "Sugarcult", "Lights Out" },
{ "Made a Mistake", "Sugarcult", "Lights Out" },
{ "Kiss You Better", "Maximo Park", "A Certain Trigger" },
{ "All Over the Shop", "Maximo Park", "A Certain Trigger" },
{ "Going Missing", "Maximo Park", "A Certain Trigger" }
};
JTable table = new JTable(data, names);
frame.add(table);
4)If I use paintComponent(Graphics g)
for JPanel then nothing is translucent,whether I add table or not or as many buttons are added.Why?
(I am asking about the immediate output when application is run.I know that when mouse is rolled over those buttons or if any row in table is clicked it will become opaque,which is due to Swing's painting mechanism.)