A GridBagLayout Example

Theory is good, but we need to put some code behind these abstract concepts. The following example, from GridBag.py, takes us step by step through the layout of components with GridBagLayout. At first we'll use only buttons; then we'll graduate to other components such as text fields and labels. To start, import the classes needed.

>>> from javax.swing import JButton, JTextField, JFrame, JButton, JPanel, JLabel
>>> from java.awt import GridBagLayout, GridBagConstraints

Constraints

Define a helper function for creating GridBagConstraints. If you're following along in the interactive interpreter, enter from GridBag import makeConstraint.

 def makeConstraint() gridx=0, gridy=0, # Holds the x, y cell location in the grid ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.