• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Sizing an application to an image

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really a beginner, but this question seems so fundamental I thought I should post it here. I have an application using a JFrame. I create a JPanel which loads an image. It registers its preferred size to the dimensions of the image. Then I add the JPanel to the JFrame and pack it. Sometimes the JFrame resizes correctly to the size of the image, but sometimes it sets its size with extra space on the right and bottom. I can set the size explicitly to the size of the image (I know its dimensions, of course) and that always works, but I'd prefer it to be data driven. Here is the relevant code:

Application:

JPanel:


Any idea what's going on? TIA!
 
Marshal
Posts: 79648
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

We usually discuss that sort of thing on the Swing forum, so I shall move you there.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris, from your code it seems that the contentpane of the JFrame has BorderLayout, and that you are adding the panel containing the image as BorderLayout.CENTER. Instinct tells me that this is the source of your problem. as I am pretty sure the amount of space allocated to Center depends on what is happening with NORTH, SOUTH, EAST,WEST.

I would experiment with different locations within BorderLayout, or with different layouts, to see if that makes a difference.

just a hunch.



 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. Since I have just one component, a JPanel, I didn't think the layout manager would matter. But I tried different layout managers anyway, just to see. It didn't help: I got the exact same results. Sometimes I have extra space at the right and bottom edges. I'll keep trying different things, but if anyone has some insight, I'd like to hear it.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I would tend to agree with what you say, if you only have one component. It's an interesting problem.

Just to summarize, you are absolutely certain the extra space is not part of the JPanel, but part of the content pane? Pack is suppose to deal with this. I thought.

The problem as you describe it should not be related to the fact you have an image on the JPanel. Because you have programatically set the preferred Dimension of the JPanel to the size of the image. Do you agree? So It should happen with any old JPanel, or not at all. This provides a direction for your detective work.

In border layout, NORTH, SOUTH, EAST, WEST take as much space as they can/need. CENTER tends to take what is left. So if you have more than one compponent, and your fram is not big enough, what is in Center will lose out, and it is center that will show extra space if any if the other four are taller/wider.

Have you tried BorderLayout.NORTH or SOUTH? I'm guessing now.

You may find this course on Layout useful, it's not part of the Java Tutorial

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html

If you solve it, let us know.
 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I seemed to have solved it. Instead of adding the JPanel to the JFrame, I did this:

But it bugs me that I shouldn't have needed to do that. I still don't know why JFrame wasn't sizing correctly around the single JPanel. I solved it, but it's not really satisfying.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with you. It shouldn't have been necessary. Is your code in the first post complete? If you feel so inclined, make the complete code of the original available, here or by private message, I'm interested in playing with it a bit myself.

regards.
 
Campbell Ritchie
Marshal
Posts: 79648
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't send code around by private message. Read this FAQ. Everybody should be able to see the code and have an opportunity to comment.
 
Chris Nash
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fred Hamilton wrote:If you feel so inclined, make the complete code of the original available. I'm interested in playing with it a bit myself.



I think I posted all the relevant portions of the code. The rest is just regular game code. Thanks for all the comments!
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris Nash wrote:

Fred Hamilton wrote:If you feel so inclined, make the complete code of the original available. I'm interested in playing with it a bit myself.



I think I posted all the relevant portions of the code. The rest is just regular game code. Thanks for all the comments!



OK, well I can't see anything wrong in what you posted, so who knows what the issue is. I'll keep an eye out for that sort of problem in my own projects.

cheers.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have you tried....panelImg.setLocation(0,0) for the panel with the image?..or panelImg.setBounds(0,0,panelImg.getWidth(),panelImg.getHeight())???
seeing your code I realized that you have created a new class for your panel (titlePanel), so you could also try :
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic