Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding repeating background to JFrame

Posted on 2008-06-17
7
Medium Priority
?
1,166 Views
Last Modified: 2008-06-24
Hi,
I have a background image that I want to repeat to fill the JFrame.  How do I go about doing that?
With the code snippet below, the windows app is only the size of the actual image.  I know I don't have size dimensions set to make it bigger or anything, but if I specified the JFrame's size to be bigger, how would I make the background image repeat to fill the frame?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
 
public class DavisPublishing 
{
	public JFrame conPanel;	
	
	public DavisPublishing(String title) 
	{
		conPanel = new JFrame();
		conPanel.setTitle(title);
 
		JLabel background = new JLabel(new ImageIcon("images/parchmnt.jpg"));
		conPanel.setContentPane(background);
		conPanel.getContentPane();
		conPanel.setLayout(new BorderLayout());
		conPanel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		conPanel.pack();
		conPanel.setVisible(true);
	}
 
   
   public static void main(String[] args)
   {
	   new DavisPublishing("Davis Publishing Company");
   }
}

Open in new window

0
Comment
Question by:P1ST0LPETE
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21804850
I'm afraid you'd have to do your own tiling of the image i think. That means overriding paint/paintComponent
0
 
LVL 3

Accepted Solution

by:
Sankozi earned 2000 total points
ID: 21807061
I think the easiest solution is using HTML:
JEditorPane background = new JEditorPane();
background.setContentType("text/html");
background.setText(
     "<html><body style=\"background-image: url(http://www.google.com/intl/en_ALL/images/logo.gif);\"></body></html>");
conPanel.setContentPane(background);

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21807995
Much better. Pleasant surprise to see that css is supported
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 21808021
(i.e. that *particular* css)
0
 
LVL 10

Author Comment

by:P1ST0LPETE
ID: 21808129
So I can use HTML in a standard Java app?  This program isn't web based at all.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21808152
Yes, most Swing components support html.
0
 
LVL 92

Expert Comment

by:objects
ID: 21808158
Use the TexturePaint class, see the example here:

http://www.objects.com.au/java/qa/418590870.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question