• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sizing a Component placed on a JFrame instead of a JPanel

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am trying a method of creating a Java program by putting components on a JFrame instead of on a JPanel and putting the JPanel on the JFrame. I found the following code, which adds a JButton to a JFrame and displays it, in an old Java book:



This code works fine but the button expands to fill the window even when I give it a size altough the book's picture of the program shows the button at proper size.
What is going on? How can I fix this?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

What was the book? Did you copy the code exactly? Did the book recommend that way of writing an ActionListener? That is not a good way to write listeners at all. They should have their own classes.
What layout did you expect to apply to the frame? I think it is a layout problem, and the layout overrides the suggested size of the button.

I shall move this discussion to our GUIs forum.
 
Rosie Fairfield
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thanks for responding.
It's a very old book. I am aware that this is not the best way to do a GUI - I normally put a JPanel in a JFrame; I simply wonder why it worked in the book and not for me.
I changed the book's code as follows:
1.
different variable names;
2.

became

3.
The book's button's action was to close the program; my program displays a message.
I created a new project and copied the code in the book exactly and it still didn't work.

I understand your comments about layouts and while that might make the program work, it doesn't explain why the book's method, with no layout, doesn't work for me. I'm wondering if Java or JFrame has changed so that this won't work now.
If anyone has an answer or wants to play around and find the answer, great, I'd love to know what you find but it's not important enough to really matter.

Thanks
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
definitely due to the default layoutManager of the frame. read about them here:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

also, layoutManagers work with setPreferredSize(...), they do not work with setSize()
the frame is OK with setSize, as no layoutManager is required to display the frame on the screen
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can’t be that old a book if it knows about add replacing getContentPane().add.
Wwhich book was it?
And which layout are you using? Hint: you are using a layout, but might not know which. It will be in the documentation for JFrame, or the Java Tutorials.
reply
    Bookmark Topic Watch Topic
  • New Topic