Skillversitas

Layout Management

Swing containers arrange their children with layout managers - you do not position components by pixel coordinates (unless you want to). Jython uses the same managers: FlowLayout, BorderLayout, GridLayout, GridBagLayout, and the Box layouts. Choosing the right manager is most of the work in a usable GUI.

FlowLayout - Left to Right

from javax.swing import JFrame, JButton
from java.awt import FlowLayout

frame = JFrame("Flow")
frame.setLayout(FlowLayout(FlowLayout.CENTER, 10, 10))  # alignment, hgap, vgap
for text in ["One", "Two", "Three"]:
    frame.add(JButton(text))
frame.setSize(320, 80)
frame.setVisible(True)

BorderLayout - Five Regions

from javax.swing import JFrame, JButton, JLabel, BorderLayout
import java.awt as awt

frame = JFrame("Border")
frame.setLayout(BorderLayout())
frame.add(JLabel("North: menu bar area"), BorderLayout.NORTH)
frame.add(JButton("South: buttons"), BorderLayout.SOUTH)
frame.add(JLabel("East: tools", awt.SwingConstants.CENTER), BorderLayout.EAST)
frame.add(JButton("West: nav"), BorderLayout.WEST)
frame.add(JLabel("Center: main content", awt.SwingConstants.CENTER), BorderLayout.CENTER)
frame.setSize(420, 240)
frame.setVisible(True)

BorderLayout is the default for JFrame; the center region absorbs all extra space.

GridLayout - Equal Cells

from javax.swing import JFrame, JButton
from java.awt import GridLayout

frame = JFrame("Grid")
frame.setLayout(GridLayout(3, 3, 4, 4))  # rows, cols, hgap, vgap
for i in range(9):
    frame.add(JButton(str(i)))
frame.setSize(240, 200)
frame.setVisible(True)

GridBagLayout - Flexible 2D

from javax.swing import JFrame, JButton, JLabel
from java.awt import GridBagLayout, GridBagConstraints
import java.awt as awt

frame = JFrame("GridBag")
frame.setLayout(GridBagLayout())
g = GridBagConstraints()
g.anchor = awt.GridBagConstraints.WEST
g.insets = awt.Insets(4, 4, 4, 4)

g.gridx, g.gridy = 0, 0
frame.add(JLabel("Name:"), g)
g.gridx = 1
frame.add(JButton("name field"), g)

g.gridx, g.gridy = 0, 1
frame.add(JLabel("Age:"), g)
g.gridx = 1
frame.add(JButton("age field"), g)

frame.setSize(320, 120)
frame.setVisible(True)

BoxLayout and Box

from javax.swing import JFrame, JButton, Box
import java.awt as awt

frame = JFrame("Box")
box = Box.createVerticalBox()
box.add(JButton("top"))
box.add(Box.createVerticalStrut(8))
box.add(JButton("bottom"))
frame.add(box, awt.BorderLayout.CENTER)
frame.setSize(200, 140)
frame.setVisible(True)

Choosing a Manager

  • Toolbar of buttons: FlowLayout.
  • Main window skeleton: BorderLayout (menu north, status south, content center).
  • Uniform grid of equal buttons: GridLayout.
  • Form with labeled fields: GridBagLayout.
  • Stacked strips: Box/BoxLayout.
  • Compose: put a BorderLayout on the frame and nest the other managers in panels for each region.

Summary

Swing layout managers arrange components for you: FlowLayout for rows, BorderLayout for the five-region window skeleton, GridLayout for equal cells, GridBagLayout for forms, and Box for simple stacks. The practical pattern is BorderLayout on the frame with sub-managers inside region panels - and in Jython all of it is just Java API calls from Python.