Changed the background of one of my tab of a JTabbedPane

Hi all.

I have a JTabbedPane with 3 tabs ('test1', 'test2', 'test 3').

I would like to change the background of 'test1' pane (not the tab) by drawing an image.

I would also like to add in 'test1' JInternlFrame (what I have done).

My problem is that I can not do the two things simultaneously. When I want to repaint the 'test1' pane, it's added a new tab.

I would like both the image (the 'test1' pane background) and the JInternalFrame to be in a same tab.

'test1', 'test2', 'test3' don't have the same component.

If someone can help me please, it would be great..

[637 byte] By [ousmanea] at [2007-11-27 5:34:33]
# 1

Thanks all.

I have found the solution.

Repaint the JDesktopPane using paintComponent will do the job.

Here is the code if someone is interested in.import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class DesktopPane extends JDesktopPane{

/**

*

*/

private static final long serialVersionUID = 1;

public static JTabbedPane desktopTab = new JTabbedPane();

public static DesktopPane desktopPane=null;

static JInternalFrame internal = new JInternalFrame("Test++",true,true,true,true);

ImageIcon m_tabimage = new ImageIcon("img/logo.png");

public DesktopPane(JInternalFrame internalFrame,String title){

super();

add(internalFrame);

desktopPane = this;

this.add(internal);

desktopTab.addTab(title,this);

setOpaque(true);

}

public JTabbedPane getDesktopTab(){

return desktopTab;

}

public void addInternalFrame(DesktopPane desktop, String title) {

desktopTab.add(desktop);

}

public void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2D =(Graphics2D)g;

Image mIcon = m_tabimage.getImage();

int aX = getWidth()/2 - mIcon.getWidth(null)/2;

int aY = getHeight()/2 - mIcon.getHeight(null)/2;

g2D.fill(new Rectangle(aX-2,aY-2,mIcon.getWidth(null)+2,

mIcon.getHeight(null)+2)); // iconWidth

Rectangle rect = new Rectangle(aX-2,aY-2,mIcon.getWidth(null)+2, mIcon.getHeight(null)+2);

g2D.draw(rect);

rect = new Rectangle(aX-1,aY-1, mIcon.getWidth(null)+1,

mIcon.getHeight(null)+1);

g2D.draw(rect);

g2D.drawImage(mIcon,aX,aY,null);

}

public static void main(String[] args){

JInternalFrame internalFrame = new JInternalFrame("Test",true,true,true,true);

internalFrame.setSize(new Dimension(300,300));

internalFrame.setVisible(true);

internalFrame.setLocation(new Point(20,20));

internalFrame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);

//desktopTab.addTab("Test", new JPanel());

internal.setSize(new Dimension(300,300));

internal.setVisible(true);

internal.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);

//desktopTab.addTab("Desktop", internal);

DesktopPane pane = new DesktopPane(internalFrame,"Desktop");

//pane.addInternalFrame(new DesktopPane(internal, "Desktop"),"Desktop");

JFrame frm = new JFrame("Test");

//Changelistener for tabbedpane

desktopTab.addChangeListener(new DesktopTabChangeListener());

JButton newButton = new JButton();

newButton.addActionListener(new NewButtonListener());

frm.getContentPane().setLayout(new BorderLayout());

frm.getContentPane().add(pane.getDesktopTab(),BorderLayout.CENTER);

frm.getContentPane().add(newButton,BorderLayout.SOUTH);

frm.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

frm.pack();

frm.setVisible(true);

}

ousmanea at 2007-7-12 15:02:45 > top of Java-index,Desktop,Core GUI APIs...