Class TLcyTwoColumnLayoutBuilder
Class which facilitates the population of a Container
with Component
s
in a two-column layout. Each of those columns can contain a "label" and "editor" component. Methods
are also available to add separators or components which span a column or the whole width of the
Container
.
Once all Component
s have been added to this builder they can be added to the
Container
by calling the populate
method. Afterwards
this builder can no longer be used.
An example usage of this builder:
//create a frame for the panel
JFrame frame = new JFrame( "TLcyTwoColumnLayoutBuilder demo" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//use the builder to add components
TLcyTwoColumnLayoutBuilder builder = TLcyTwoColumnLayoutBuilder.newBuilder();
builder.addTitledSeparator( "Label-editor" );
builder.row().
columnOne( new JLabel( "First name"), new JTextField( 15 ) ).
columnTwo( new JLabel( "Last name" ), new JTextField( 15 ) ).build();
builder.addTitledSeparator( "Full width span" );
JPanel textAreaPanel = new JPanel( new BorderLayout() );
textAreaPanel.add( new JScrollPane( new JTextArea(15,50) ), BorderLayout.CENTER );
builder.row().
spanBothColumns( textAreaPanel ).
growVertically( true ).
build();
builder.addTitledSeparator( "Half width span" );
builder.row().
columnOne( new JTextField( "Left span", 30 ) ).
build();
builder.row().
columnTwo( new JTextField( "Right span", 30) ).
build();
//create the panel and use the builder to add the components and initialize the layout
JPanel panel = new JPanel( );
builder.populate( panel );
//add the panel to the frame and show the UI
frame.getContentPane().add( panel );
frame.pack();
frame.setVisible( true );
which results in the following UI:
You can also use this layout to create panels containing only a single column by placing all the components in the first column, as shown below:
//create a frame for the panel
JFrame frame = new JFrame( "Single column demo" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//use the builder to add components
TLcyTwoColumnLayoutBuilder builder = TLcyTwoColumnLayoutBuilder.newBuilder();
builder.addTitledSeparator( "Label-editor" );
builder.row().
columnOne( new JLabel( "First name"), new JTextField( 15 ) ).build();
builder.row().
columnOne( new JLabel( "Last name" ), new JTextField( 15 ) ).build();
//create the panel and use the builder to add the components and initialize the layout
JPanel panel = new JPanel( );
builder.populate( panel );
//add the panel to the frame and show the UI
frame.getContentPane().add( panel );
frame.pack();
frame.setVisible( true );
which results in the following UI:
- Since:
- 2012.0
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Class following the Builder pattern which allows to add a row to a two column layout. -
Method Summary
Modifier and TypeMethodDescriptionaddTitledSeparator
(String aSeparatorTitle) Adds a titled separator which will span the whole widthstatic TLcyTwoColumnLayoutBuilder
Create a new builder instancevoid
PopulateaContainer
with allComponent
s previously added to this builder.row()
Returns a builder for one row of this layout.
-
Method Details
-
newBuilder
Create a new builder instance- Returns:
- a new builder instance
-
addTitledSeparator
Adds a titled separator which will span the whole width- Parameters:
aSeparatorTitle
- The title for the separator. May be empty but must not benull
- Returns:
- this builder instance, allowing to chain method calls
-
populate
Populate
aContainer
with allComponent
s previously added to this builder. The layout of those components will be as specified when the components were added to this builder.After calling this method, this builder can no longer be used.
Warning: this method will remove all
Component
s which were previously added toaContainer
.- Parameters:
aContainer
- The container. Must not benull
-
row
Returns a builder for one row of this layout. Once all the components are specified on the
RowBuilder
, use theRowBuilder#build()
method to get a reference back to the updated version of this builder.The class javadoc contains a sample snippet illustrating the use of this method.
- Returns:
- a row builder for this builder
- See Also:
-