Username:
Password:
Java 6 Tutorials

Swing Components / XY Layout

Subtitles of the Movie

If you know exactly how you want a window to be laid out, if you know the size and position of all the components, you can organize the window without using a layout manager. If you're really picky, this is about the only way to get exactly what you want. I've done it this way by sketching the window on a sheet of graph paper before laying it out and it worked out quite well. You will need to import the classes for insets and dimension because you'll use those to hold the measurements you get from the window and the components. To build a frame, you first get the content pane from the JFrame window and set its layout manager to null. You do this because any layout manager would interfere with your positioning. You need to get the insets. These are the borders around the windows. The JFrame places border decoration all around the window and in particular, has a large inset at the top for the bar it places across there. Then it's a matter of creating a button and setting its size. It's the job of the layout manager to set the size of the components and you're doing the job of the layout manager. Every component has a preferred size and this example sets the components to their preferred size. They also have a minimum size and a maximum size, but there's usually no need to go there. The set bounds method sets the XY position of the upper left-hand corner button, as well as its size. Finally, the button is added to the pane, the same as it would be if there were a layout manager. The pac method is not called because there is no layout manager. Instead, the set size method is called to size the window. The X and Y size of the window is calculated to take into consideration the insets on the four sides of the window. That way we get the actual size of the visible portion of the window. Here's the result. You can use this technique to place any number of any kind of components exactly where you want them. My experience has been that most windows can be laid out just fine using one of the existing layout managers, but there seems to be a rule that in any large application, there's at lease one window that has to be laid out this way. One important point to consider is that this layout is measured to fit exactly one window's size. Most layout managers are flexible enough to do something intelligent if the window is resized. But if you want to layout your own window this way and also allow for it to be resized, you have a lot more work to do. Your job then becomes one of checking the new window size and using that to figure out where things go.

Tutorial Information

Course: Java 6
Author: Arthur Griffith
SKU: 33858
ISBN: 1-934743-59-3
Release Date: 2008-02-29
Duration: 7 hrs / 92 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 81,350 Video Tutorials (20,800 free)
  • Video Available as Flash or QuickTime
  • Over 782 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available