• Post Reply Bookmark Topic Watch Topic
  • New Topic

Background image doesn't appear on window  RSS feed

 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am trying to add a background image to a form and it has instead increased the size of the window to fullscreen.

The code for the background image:




Here is the full code of the form:



Main class



I have posted a pic of how it looks before adding the background image but I would like to however increase the size of the window. When I call f.setSize(800, 600) in the main, the JLabels and JTextArea shrink to size 0.

Thanks!
Screenshot-(256).png
[Thumbnail for Screenshot-(256).png]
This is how it looks after adding the background image
Screenshot-(257).png
[Thumbnail for Screenshot-(257).png]
This is how it looks before adding the background image
Screenshot-(252).png
[Thumbnail for Screenshot-(252).png]
That's the size I want it to appear with a background image
 
Dave Tolls
Ranch Hand
Posts: 2839
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're calling pack() before you add the panel with the background image.
Try moving the pack() call to after adding the panel.

pack() gets the JFrame to organise all the parts, and size them correctly, so it should be called after everything has been added.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dave Tolls Thanks but that didn't make much difference
Screenshot-(258).png
[Thumbnail for Screenshot-(258).png]
Here is how it looks after I modified the code as per what you said
 
Dave Tolls
Ranch Hand
Posts: 2839
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's odd (especially since I see you do a pack in main() after creating your frame).

What happens if you completely remove that pack call?
I don't actually see the need for it, since the later pack() should do the job.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what happens when I remove the pack() call.
Screenshot-(259).png
[Thumbnail for Screenshot-(259).png]
 
Dave Tolls
Ranch Hand
Posts: 2839
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the pack inside the try/catch block has gone, but the pack() call in main() is still there?
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sophia,

You need to learn to simplify your code when attempting to understand new concepts. You have way too much code to see the problem. That is your code should be posted in the form of a SSCCE. In this case all you need is a frame, the panel with the background image and then a button (or some other component) that you add to the background.

You already have a working version of this code. In your other posting you use the JLabel as the background and you have a setContentPane() statement to make the label the content pane.

The concept here is the same. You want to use your background panel as the content pane.

Swing is based on a parent child relationship. In your case you want something like:

1) frame
2) background panel
3) panel with components

So in your other posting you did:



This does the parent / child relationship for you. To understand this hierarchy read the section from the Swing tutorial on Using Top Level Containers.

Or you could also have created this structure by doing:



Also, you are never forced to use a single panel with a single layout manager for your form. In a previous question you asked about displaying a button at the top and a footer at the bottom. Well you could easily use a panel with a BorderLayout. Then you add a panel using a FlowLayout that is right aligned and contains a button that you add to the PAGE_START. Then you can add your footer label to the PAGE_END. This label can contain Borders to give you your spacing and line separator. Finally in the CENTER you create another panel with other components. That is break your form into logical panels to make it easier to maintain.

 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:

Or you could also have created this structure by doing:





But doesn't this piece of code does what you just said:



And then I added the components to panels like as in:

 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:So the pack inside the try/catch block has gone, but the pack() call in main() is still there?


Yes, the pack() is the main was still there and then I took that screenshot
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But doesn't this piece of code does what you just said:


Yes, that adds the background to the frame.

But then you never add any components to the background.

Look at my code. I specifically create a "background" variable and add other components to the background.

And then I added the components to panels like as in:


But you never add the "pnl" variable to the "p" variable.

If you want more help understanding this concept then you need to create a proper `SSCCE`. This is a basic concept to learn and there is no need to write hundreds of lines of code to learn it. The SSCCE would be about 20 lines of code (or less).
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sophia,

Listen to what Rob Camick is saying, you make a frame and some panels, but the panel that is painted with the background is, essentially, just stuffed into your project, it is not used for anything. Your packs need to be AFTER components are added to your display object so Java can set the appropriate orientation for them. The way you have your project setup, you have your panels on top of a frame. To add a image for the entire page, which is what I think you want to do, you need to set all of your component backgrounds to transparent except for the bottom most, and then render to that object's graphics context, in your case that would be the JFrame.

Rendering to a JFrame is not recommended, especially at your level of Java skill, so you would need to add a JPanel to the JFrame that would be the size you want to work with, then add everything to that JPaenl.

The method I have showed you to add the background has been used successfully for decades now, and will work just fine when you actually use the JPanel you set the background on.

Les
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob Camick
Thanks a lot for the advice. Will bear that in mind for my future posts. Actually, I post the whole code in case someone wants to run it to see how it works or to make something work.

I did as you said through the following codes:



I added that snippet of code at the end i.e after these lines



The background image appeared but the layout of the frame has somehow changed and the window size has increased to full size. Any fixes for this?

Screenshot-(268).png
[Thumbnail for Screenshot-(268).png]
This is how it looks now
 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sophia,

You have to set the layout in p and not in your JFrame. You are using p now as your container for your form. What you are seeing is the default FlowLayout of the JPanel, p.

Les
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I post the whole code in case someone wants to run it to see how it works or to make something work.


You missed the point of my comment.

A SSCCE is meant for you to strip out all the code that is NOT directly related to the stated problem. The remaining code will be executable.

In this case you have a frame, a background panel and a component added to the background panel. The whole SSCCE would be about 20-30 lines of code. Much easier to debug and understand.

Any time you have 14 replies to a question you know the question/code is not clear. The whole point of the SSCCE is to simplify the entire question and code.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Les Morgan

So I set the layout in p:



And it threw an Illegal Argument Exception.
I am really sorry for all this trouble. It's my first GUI actually ..

 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And it threw an Illegal Argument Exception.


On what statement?

I can make a guess, but you need to learn how to ask a proper question and give us the information we need to solve the problem.

Also, did you think about what the Exception means? Or did you just post the question in the forum?

You have used a GridBagLayout before. How is your usage different this time???

It's my first GUI actually ..


Which is why you take the time to create a SSCCE. You need to learn to walk before you run. You need to understand the basic concepts BEFORE you move on to more complex concepts.

As I told you in my last answer which you completely ignored, if you simplify the problem by just using a frame a background panel and another component you will be learning a concept, not trying to solve an application problem. Once you understand the concept you have a simple knowledge that allows you to make a more complicated GUI. But you have to start simple first. One step at a time. It has been 4 days and it is a very simple concept to understand the parent/child relationship between all Swing components. If you don't understand that you can't build a more complicated GUI.
 
Sophia Green
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I had commented out the line when I set the layout in p and that affected the GridBagConstraints gbc here:
I set the layout in the JFrame again(and now it doesn't throw any exceptions) and maintained the layout in p as in the code snippet I had posted earlier. When I ran it, it didn't solve the problem tho.

I do understand what you meant. It's just that there are many components and I am still learning.
Thanks for all the advice. I'll bear that in mind and try to improve my knowledge on the parent/child relationship between Swing components.

Screenshot-(278).png
[Thumbnail for Screenshot-(278).png]
Here is how it looks now:
 
Rob Camick
Ranch Hand
Posts: 2788
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do understand what you meant. It's just that there are many components and I am still learning.


No you don't. You posted two random lines of code. In the context of a 200 line program that doesn't give us any information.

The point of a SSCCE is to create a 20-30 line program that still demonstrates the concept of what you are trying to do.

For example:


Why are you adding multiple panels with multiple components to the background???. Who cares how complex each panel is. It is irrelevant to the actual problem you are trying to solve. The "concept" you are trying to solve is to add ANY component to the background. Once you understand this then you can add your panel.

So your above code could be simplified by doing something like:



No need for complex panel, just three buttons that shows you want the components displayed vertically. So now you add the code to create the frame and add the background to the frame and you have a 20-30 line SSCCE.

All you need to do is figure out what the constraints should be for each component you add to the background panel.

Try it and post your SSCCE that shows what you have done. Then once it is working apply knowledge to your real program.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!