Java Reference
In-Depth Information
Note GridBagLayout is one of the most useful layout managers available, and it is capable of far more
than we can show in one section of one chapter. Unfortunately, going into all its capabilities is beyond the
scope of this topic.
The Common Database Location Frame
Now that the basics of the GridBagLayout have been explained, we can show the code that cre-
ates the common frame for displaying the database location.
The ConfigOptions constructor stores a local variable to show what mode it was config-
ured with, and then constructs a GridBagLayout with a gap between components of 2 pixels in
every direction.
Regardless of which mode we are in, we will be displaying the label for the Database loca-
tion field, so we add that next.
The remaining options are configured depending on the mode we are in. If a component
or option does not make sense in the mode we are in, then it is not added to the panel. If it
does make sense to add it, then it is added, and the tooltip text is set relevant to the mode.
Listing 8-28 demonstrates how the components used within a particular mode are laid out
using the GridBagLayout .
Listing 8-28. The ConfigOptions Constructor
public ConfigOptions(ApplicationMode applicationMode) {
super();
this.applicationMode = applicationMode;
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
this.setLayout(gridbag);
// Standard options
// ensure there is always a gap between components
constraints.insets = new Insets(2,2,2,2);
// Build the Data file location row
JLabel dbLocationLabel = new JLabel(DB_LOCATION_LABEL);
gridbag.setConstraints(dbLocationLabel, constraints);
this.add(dbLocationLabel);
if (applicationMode == ApplicationMode.NETWORK_CLIENT) {
locationField.setToolTipText(DB_IP_LOCATION_TOOL_TIP);
constraints.gridwidth = GridBagConstraints.REMAINDER; //end row
} else {
locationField.setToolTipText(DB_HD_LOCATION_TOOL_TIP);
// next-to-last location in row
constraints.gridwidth = GridBagConstraints.RELATIVE;
Search WWH ::




Custom Search