Transparent JTabbedPane

I have a background image that I need to show through everything. I created a JTabbedPane on top of the background and it was still fine, but as soon as I put a JPanel into the JTabbedPane it is no longer transparent like I need it. I have set both the JTabbedPane and the JPanel inside to not be opaque. All I can see is the light blue of the selected tab though. Is there a way to make it all completely transparent? Here is my code: JPanel main =new JPanel(){

ImageIcon background =new ImageIcon(getClass().getResource("/com/site/Graphics/background.jpg"));

protectedvoid paintComponent(Graphics g){

g.drawImage(background.getImage(),0,0,null);

}

};

JTabbedPane tab =new JTabbedPane();

tab.setOpaque(false);

JPanel panel =new JPanel();

panel.setOpaque(false);

tab.addTab("Panel", panel);

main.add(tab);

pack();

[1350 byte] By [drawimagea] at [2007-11-27 10:52:22]
# 1

I'm no expert here, but the problem it seems is not with the JPanel but with the JTabbedPane. Might you have to override its paintComponent method and redraw a portion of your image (depending on where the JTabbedPane is placed in your JPanel) on the JTabbedPane? Just a SWAG though.

petes1234a at 2007-7-29 11:37:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thank you for the response, I believe I need to do this also. I looked at the JTabbedPane class and could not find the paint() method. Do you happen to know where it is?

drawimagea at 2007-7-29 11:37:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

not sure this is what you're trying to do, but worth a look

import java.awt.*;

import javax.swing.*;

class Testing

{

public void buildGUI()

{

JPanel main = new JPanel(new BorderLayout()) {

ImageIcon background = new ImageIcon(getClass().getResource("test.gif"));

protected void paintComponent(Graphics g) {

g.drawImage(background.getImage(),0,0,this.getPreferredSize().width,this.getPreferredSize().height,null);

}

};

JTabbedPane tab = new JTabbedPane();

tab.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI(){

protected void paintTabBackground(Graphics g,int tabPlacement,int tabIndex,int x,int y,int w,int h,boolean isSelected){}

protected void paintContentBorder(Graphics g,int tabPlacement,int selectedIndex){}

});

JPanel panel = new JPanel(new GridBagLayout());

panel.setOpaque(false);

panel.setPreferredSize(new Dimension(600,600));

panel.add(new JLabel("Hello World"),new GridBagConstraints());

tab.addTab("Panel", panel);

main.add(tab);

JFrame f = new JFrame();

f.getContentPane().add(main);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-29 11:37:06 > top of Java-index,Desktop,Core GUI APIs...
# 4

Thank you for your response, I believe this will help me achieve my desired result.

drawimagea at 2007-7-29 11:37:06 > top of Java-index,Desktop,Core GUI APIs...
# 5

That worked perfectly, thank you so much Michael_Dunn.

drawimagea at 2007-7-29 11:37:06 > top of Java-index,Desktop,Core GUI APIs...