954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Display image for background

I have a JTree, the renderer uses JLabels. If I setOpaque(false), then I get display issues with highlighting tree nodes/leafs, so I keep the rendering of the jtree opaque. I'd like to display an image for the background and have the text from the jtree elements displayed over top of it. The JTree is in a JScrollPane. I've tried using the image as the background for both the JFrame or the JPanel. (which the scrollpane is in) I am painting the background properly as far as I'm aware.

(JFrame)
public void paint(Graphics g)
{
	super.paint(g);
	g.drawImage(img,0,0,this);
}


Here's how it looks now.
[img]http://img.photobucket.com/albums/v204/phaelax/javabackground.jpg[/img]

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

I can get everything to display properly if I don't use a JScrollPane. setOpaque(false) on the scroll pane doesn't seem to change anything.

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

have you tried setting the background of the JScrollPane? Might require a custom JScrollPane descendent to draw it, I never attempted anything of the kind.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

I'd like to recommend putting the drawing in a seperate class. It always seems to make errors easier to fix that way.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

I made an ImagePanel to host the background image. The JScrollPane has a viewport, which seems to act sort of like a JPanel (visually) underneath the actual component that you see. Setting the viewport to no be opaque didn't work, so I added my tree to an ImagePanel then set my viewport to the image panel.

p.s. Nice java links server_crash, that wireless was a nice read

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 
I made an ImagePanel to host the background image. The JScrollPane has a viewport, which seems to act sort of like a JPanel (visually) underneath the actual component that you see. Setting the viewport to no be opaque didn't work, so I added my tree to an ImagePanel then set my viewport to the image panel.

So the Image is actually covering up the scrollpane? What are you adding to the scrollpane?
Do you mind posting a picture of the app. without the image so that we can see the scrollpane?

[qoute]
p.s. Nice java links server_crash, that wireless was a nice read[/QUOTE]
Thanks ;) I probably should change some of the text colors!

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Here's an image with the background image. The image is part of the ImagePanel. The image is centered in the panel, with background for the rest of the visible panel set to white.
[IMG]http://img.photobucket.com/albums/v204/phaelax/programming_projects/aimclient_groups.jpg[/IMG]

ImagePanel

public class ImagePanel extends JPanel
{
	private Image image = null;
	private int iWidth2;
	private int iHeight2;

	public ImagePanel(Image image)
	{
		this.image = image;
		this.iWidth2 = image.getWidth(this)/2;
		this.iHeight2 = image.getHeight(this)/2;
	}
	
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if (image != null)
		{
			int x = this.getParent().getWidth()/2 - iWidth2;
			int y = this.getParent().getHeight()/2 - iHeight2;
			g.drawImage(image,x,y,this);
		}
	}
}

The reason why I get the size of the parent is because this panel is being set as a scrollpane's viewport, which can be much larger than what you visually see inside the scroll pane.

Here's the order of how everything is put together. The code should make more sense than my explanations.

buddyTree.setRootVisible(false);
		buddyTree.setShowsRootHandles(false);
		buddyTree.putClientProperty("JTree.lineStyle", "None");
		buddyTree.setCellRenderer(new BuddyTreeCellRenderer());
		
		JScrollPane sp = new JScrollPane();
		(sp.getViewport()).setOpaque(false);
		sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	   
		ImagePanel iPanel = new ImagePanel(img);
		iPanel.setLayout(new BorderLayout());
		iPanel.add(buddyTree,BorderLayout.CENTER);
		iPanel.setBackground(Color.white);
		sp.getViewport().setView(iPanel);
		
		buddyTree.setOpaque(false);
		iPanel.setOpaque(true);
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(sp, BorderLayout.CENTER);
Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You