permaculture playing cards*
The moose likes Swing / AWT / SWT and the fly likes Component Order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Component Order" Watch "Component Order" New topic
Author

Component Order

Daniel Gen Li
Ranch Hand

Joined: Jan 02, 2010
Posts: 32
I've read a topic about the easiest way to put a picture on a JFrame. So I used a JLabel-ImageIcon method.

My goal was to place that image as a background image behind all of my GUI components. I've added the JLabel first, and then everything else. However, when I run the program, the picture covers everything. All the other components are still there because when I click on my button, it shows up. But unless I click on them, they are being hidden by the image (or should I say JLabel).

So how can I control the order of which the elements draw? Or even more generally, how to control the order of components? Is there a method that does this?


Li
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
Background Panel explains how you can get the JLabel approach to work, or it provides and alternate solution of drawing the image yourself.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Daniel Gen Li wrote: (...)My goal was to place that image as a background image behind all of my GUI components.(...)

here's an example:


java amateur
Daniel Gen Li
Ranch Hand

Joined: Jan 02, 2010
Posts: 32
Thank you very much miguel lisboa, I looked at your example and then used the paintComponent method. It works like a charm.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
I can see that neither of you looked at my solution since I showed you how to do it in a couple lines of code without creating a custom class.

Also, it is never a good idea to read the file in the paintComponent() method since you don't want to be reading the image every time the component is painted.
Daniel Gen Li
Ranch Hand

Joined: Jan 02, 2010
Posts: 32
Rob Camick wrote: Also, it is never a good idea to read the file in the paintComponent() method since you don't want to be reading the image every time the component is painted.

Of course, that's is absolutely true. So I put a g.drawImage in the paintComponent method.

I do not mean to start an argument, I just think you have made a misunderstanding. I did not ignore your solution. I did look on the website, but miguel lisboa's example had inspired a simpler solution for my case (at least for my level of knowledge on Java it made more sense). And of course I appreciate your effort to help, thank you Rob.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
Rob Camick wrote:I can see that neither of you looked at my solution since I showed you how to do it in a couple lines of code without creating a custom class.

Also, it is never a good idea to read the file in the paintComponent() method since you don't want to be reading the image every time the component is painted.

hi Rob: i'm glad you'v commented what i posted
the idea got its inspiration in Head First Java's book
still in the book they dont use an inner class
instead they write it like this:

but in this case the image doesnt load, i dont understand why
one last question respecting my code: you recommend creating the image outside, once?



Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
but in this case the image doesnt load, i dont understand why


I don't know why the image doesn't load. It shouldn't matter whether you are using an inner class or normal class.

PanelWithRobsBackground


Yep, thats what I was suggesting you might be able to use. If you add the line:



then that solution is the same as your original solution in that the image is always painted at the top/left even when you resize the frame.

On the other hand if you need the image to automatically resize at the frame size changes then you do need to create a custom panel and use the following mehtod:



So its nice to know both solutions and understand when to use each solution.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
@ Rob
i tried to play around with your class
BackgroundPanel panel =
new BackgroundPanel(duke, BackgroundPanel.ACTUAL, 1.0f, 0.5f);

my real code uses BoxLayout and somehow i cant use your BackgroundPanel class (i know, i could extend it) precisely because of layouts conflict
am i missing something?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
i cant use your BackgroundPanel class (i know, i could extend it) precisely because of layouts conflict. am i missing something?


A panel doesn't have a preferred size. The size of a panel is based on the preferred size of the components added to it.

The BackgroundPanel was designed to be used as the background for an entire frame which is why it is added to the center of the content pane. That way it was change in size automatically as the frame changes size and the preferred size doesn't matter.

If you are using it in a layout manger that needs a preferred size then you need to set the preferred size manually equal to the size of the image. But then there is no real need to do this because as you saw above you can just use a JLabel for this purpose.

So this class is meant to be "added to" a panel that uses a layout manager that changes sizes dynamically like the BorderLayout. I guess thats not obvious from the blog entry. It should work with the BoxLayout it you set the preferred and maximum sizes, then it should also grow dynamically. You can also change the layout manager of the BackgroundPanel to be a BoxLayout and then add your components to it.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
thanks, Rob
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Component Order
 
Similar Threads
Thumbnail view in swing
How to change image on label without using seticon method
image in glassPane
Getting a JLabel to follow the cursor
Inheritance nightmare.