Package io.github.jonestimd.swing.layout
Class GridBagBuilder
- java.lang.Object
-
- io.github.jonestimd.swing.layout.GridBagBuilder
-
public class GridBagBuilder extends java.lang.Object
A form builder that simplifies usage ofGridBagLayout
. The initial constraints are as follows(gridx, gridy) =
(0, 0)fill = HORIZONTAL
weightx, weighty =
1.0anchor = WEST
GridBagFormula
when each component is added.By default, the following components are automatically wrapped in a
JScrollPane
. Automatic wrapping of components inJScrollPane
can be enabled/disabled usinguseScrollPane(Class)
andnoUseScrollPane(Class)
JTable
JList
JTextArea
-
-
Field Summary
Fields Modifier and Type Field Description static int
RELATED_GAP
static int
UNRELATED_GAP
-
Constructor Summary
Constructors Constructor Description GridBagBuilder(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix)
Create a new builder using 2 columns and the defaultGridBagFormula
s.GridBagBuilder(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix, int columns)
Create a new builder using the defaultGridBagFormula
s.GridBagBuilder(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix, int columns, java.util.Map<java.lang.Class<?>,GridBagFormula> fieldConstraints)
Create a new builder using specificGridBagFormula
s.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends javax.swing.JComponent>
Tappend(java.lang.String labelKey, T field)
Append a labeled component to the form.<T extends javax.swing.JComponent>
Tappend(java.lang.String labelKey, T field, GridBagFormula formula)
Append a labeled component to the form.void
append(javax.swing.JRadioButton... buttons)
Append a group of radio buttons in a horizontal box.<T extends javax.swing.JComponent>
Tappend(T field)
Append an unlabeled component to the form.<T extends javax.swing.JComponent>
Tappend(T field, GridBagFormula formula)
Append an unlabeled component to the form.javax.swing.JLabel
getLastLabel()
GridBagBuilder
insets(int top, int left, int bottom, int right)
Set the insets for the next component.void
nextCell()
Move to the next cell.GridBagBuilder
noUseScrollPane(java.lang.Class<? extends javax.swing.JComponent> componentClass)
Disable automatic wrapping of instances of the class in aJScrollPane
.<T extends javax.swing.JComponent>
Toverlay(T field)
Add a component in the same location as the previous component.GridBagBuilder
relatedGap()
Use the horizontal and verticalRELATED_GAP
for the next component.GridBagBuilder
setConstraints(java.lang.Class<?> componentClass, GridBagFormula constraints)
GridBagBuilder
unrelatedVerticalGap()
Use the verticalUNRELATED_GAP
and horizontalRELATED_GAP
for the next component.GridBagBuilder
useScrollPane(java.lang.Class<? extends javax.swing.JComponent> componentClass)
Automatically wrap instances of the class in aJScrollPane
.
-
-
-
Field Detail
-
RELATED_GAP
public static final int RELATED_GAP
- See Also:
- Constant Field Values
-
UNRELATED_GAP
public static final int UNRELATED_GAP
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GridBagBuilder
public GridBagBuilder(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix)
Create a new builder using 2 columns and the defaultGridBagFormula
s.- Parameters:
container
- the form containerbundle
- provides label textresourcePrefix
- prefix for resource bundle keys- See Also:
FormElement
-
GridBagBuilder
public GridBagBuilder(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix, int columns)
Create a new builder using the defaultGridBagFormula
s.- Parameters:
container
- the form containerbundle
- provides label textresourcePrefix
- prefix for resource bundle keyscolumns
- the number of grid columns
-
GridBagBuilder
public GridBagBuilder(java.awt.Container container, java.util.ResourceBundle bundle, java.lang.String resourcePrefix, int columns, java.util.Map<java.lang.Class<?>,GridBagFormula> fieldConstraints)
Create a new builder using specificGridBagFormula
s.- Parameters:
container
- the form containerbundle
- provides label textresourcePrefix
- prefix for resource bundle keyscolumns
- the number of grid columnsfieldConstraints
- map of component class toGridBagFormula
-
-
Method Detail
-
useScrollPane
public GridBagBuilder useScrollPane(java.lang.Class<? extends javax.swing.JComponent> componentClass)
Automatically wrap instances of the class in aJScrollPane
.
-
noUseScrollPane
public GridBagBuilder noUseScrollPane(java.lang.Class<? extends javax.swing.JComponent> componentClass)
Disable automatic wrapping of instances of the class in aJScrollPane
.
-
getLastLabel
public javax.swing.JLabel getLastLabel()
- Returns:
- the most recently created field label
-
setConstraints
public GridBagBuilder setConstraints(java.lang.Class<?> componentClass, GridBagFormula constraints)
-
relatedGap
public GridBagBuilder relatedGap()
Use the horizontal and verticalRELATED_GAP
for the next component.- Returns:
- this builder
-
unrelatedVerticalGap
public GridBagBuilder unrelatedVerticalGap()
Use the verticalUNRELATED_GAP
and horizontalRELATED_GAP
for the next component.- Returns:
- this builder
-
insets
public GridBagBuilder insets(int top, int left, int bottom, int right)
Set the insets for the next component.- Returns:
- this builder
-
append
public <T extends javax.swing.JComponent> T append(java.lang.String labelKey, T field)
Append a labeled component to the form.- Type Parameters:
T
- the class of the component- Parameters:
labelKey
- the resource key for the labelfield
- the component- Returns:
- the component
-
append
public <T extends javax.swing.JComponent> T append(java.lang.String labelKey, T field, GridBagFormula formula)
Append a labeled component to the form.- Type Parameters:
T
- the class of the component- Parameters:
labelKey
- the resource key for the labelfield
- the componentformula
- the constraints calculator for the component- Returns:
- the component
-
append
public <T extends javax.swing.JComponent> T append(T field)
Append an unlabeled component to the form.- Type Parameters:
T
- the class of the component- Parameters:
field
- the component- Returns:
- the component
-
append
public <T extends javax.swing.JComponent> T append(T field, GridBagFormula formula)
Append an unlabeled component to the form.- Type Parameters:
T
- the class of the component- Parameters:
field
- the componentformula
- the constraints calculator for the component- Returns:
- the component
-
overlay
public <T extends javax.swing.JComponent> T overlay(T field)
Add a component in the same location as the previous component.- Returns:
- the component
-
append
public void append(javax.swing.JRadioButton... buttons)
Append a group of radio buttons in a horizontal box.- Parameters:
buttons
- the buttons to append
-
nextCell
public void nextCell()
Move to the next cell. If there are columns left in the row then moves to the next column. Otherwise, moves to the first column of the next row.
-
-