File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Sizing a Component placed on a JFrame instead of a JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Sizing a Component placed on a JFrame instead of a JPanel" Watch "Sizing a Component placed on a JFrame instead of a JPanel" New topic
Author

Sizing a Component placed on a JFrame instead of a JPanel

Rosie Fairfield
Greenhorn

Joined: Nov 30, 2012
Posts: 16
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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40054
    
  28
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
Greenhorn

Joined: Nov 30, 2012
Posts: 16
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
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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
Sheriff

Joined: Oct 13, 2005
Posts: 40054
    
  28
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sizing a Component placed on a JFrame instead of a JPanel