Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

add and BorderLayouts  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a class that extends JFrame. Somewhere in this class I have the following piece of code.



Could somebody please help me with this particular statement.
add(<one of these objects of a class that extends JComponent - sort of a custom canvas >, BorderLayout.NORTH);

what is happening exactly when I type the above statement? The description of the method says - 'appends the specified component to the end of the container'. So the specified component must be the custom canvas and the container is the this object. Would that be right? So here that is SomeClass object. Right? In effect it is the JFrame. So is this statement adding a custom canvas to the JFrame? What is the purpose of BorderLayout.North? Is this part specifying where in the JFrame, a canvas is to be added?

Please advise.
Thanks,
Chan.

 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Effectively yes, the component is your custom canvas and the container is the SomeClass object. The statement is adding a component to the JFrame, and it expects to be using a layout called the BorderLayout.

Java provides a variety of layout managers that handle placing your components within your window. They vary from the default FlowLayout, which just puts the components one after another in a line, to the highly complicated GridBagLayout, which allows you to divide the frame up into an arbitrary number of cells of potentially different sizes. The BorderLayout is one of the simpler ones. To use it, you need to call setLayout() with a BorderLayout instance, to change the layout from the FlowLayout to a BorderLayout.

When adding components to a frame with a border layout, the component is placed along one of the edges and stretched to fit, or placed in the middle of the frame and stretched to fit. You add the component to the frame with an argument of where the component should be placed. In your example, it is placed against the top of the frame and stretched horizontally to fit the left and right edges.

The Oracle tutorial may provide some more info (keep in mind that PAGE_START is preferred over NORTH, PAGE_END is preferred over SOUTH in their documentation because it can be customized to different languages better)
http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Tina said, JPanel has a default FlowLayout; JFrame (or rather its contentPane) has a default BorderLayout.

Also, read the API for JFrame:
As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tina, Darryl. That was indeed very helpful.

Greetings,
Chan.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!