Hello, I have looked at an example of GridBagLayout
(JamesCherrill you must be thrilled : - )!!) and there are a few things that are not entirely clear to me. First here is the code taken from deitel and deitel “Java how to program”, chapter 25:
// Fig. 25.21: GridBagFrame.java
// Demonstrating GridBagLayout.
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class GridBagFrame extends JFrame
{
private GridBagLayout layout; // layout of this frame
private GridBagConstraints constraints; // constraints of this layout
// set up GUI
public GridBagFrame()
{
super( "GridBagLayout" );
layout = new GridBagLayout();
setLayout( layout ); // set frame layout
constraints = new GridBagConstraints(); // instantiate constraints
// create GUI components
JTextArea textArea1 = new JTextArea( "TextArea1", 5, 10 );
JTextArea textArea2 = new JTextArea( "TextArea2", 2, 2 );
String[] names = { "Iron", "Steel", "Brass" };
JComboBox comboBox = new JComboBox( names );
JTextField textField = new JTextField( "TextField" );
JButton button1 = new JButton( "Button 1" );
JButton button2 = new JButton( "Button 2" );
JButton button3 = new JButton( "Button 3" );
// weightx and weighty for textArea1 are both 0: the default
// anchor for all components is CENTER: the default
constraints.fill = GridBagConstraints.BOTH;
addComponent( textArea1, 0, 0, 1, 3 );
// weightx and weighty for button1 are both 0: the default
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent( button1, 0, 1, 2, 1 );
// weightx and weighty for comboBox are both 0: the default
// fill is HORIZONTAL
addComponent( comboBox, 2, 1, 2, 1 );
// button2
constraints.weightx = 1000; // can grow wider
constraints.weighty = 1; // can grow taller
constraints.fill = GridBagConstraints.BOTH;
addComponent( button2, 1, 1, 1, 1 );
// fill is BOTH for button3
constraints.weightx = 0;
constraints.weighty = 0;
addComponent( button3, 1, 2, 1, 1 );
// weightx and weighty for textField are both 0, fill is BOTH
addComponent( textField, 3, 0, 2, 1 );
// weightx and weighty for textArea2 are both 0, fill is BOTH
addComponent( textArea2, 3, 2, 1, 1 );
} // end GridBagFrame constructor
// method to set constraints on
private void addComponent( Component component,
int row, int column, int width, int height )
{
constraints.gridx = column; // set gridx
constraints.gridy = row; // set gridy
constraints.gridwidth = width; // set gridwidth
constraints.gridheight = height; // set gridheight
layout.setConstraints( component, constraints ); // set constraints
add( component ); // add component
} // end method addComponent
} // end class GridBagFrame
and the test class
// Fig. 25.22: GridBagDemo.java
// Demonstrating GridBagLayout.
import javax.swing.JFrame;
public class GridBagDemo
{
public static void main( String[] args )
{
GridBagFrame gridBagFrame = new GridBagFrame();
gridBagFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
gridBagFrame.setSize( 300, 150 ); // set frame size
gridBagFrame.setVisible( true ); // display frame
} // end main
} // end class GridBagDemo
Right, in principle, it’s clear, but here are my questions:
1)why are they not using any JPanel
and attaching everything to the JFrame
? IS that for simplicity or for other reasons (like you can’t use JPanels
with GridBagLayout
)?
2)I have read about the various constraints of type GridBagConstraints
that we can use with this layout manager, like fill
, gridx
, gridy
etc. Now, in the program the components textArea1 and button1 (among othe others) have both a fill
constraint set:
constraints.fill = GridBagConstraints.BOTH;
addComponent( textArea1, 0, 0, 1, 3 );
// weightx and weighty for button1 are both 0: the default
constraints.fill = GridBagConstraints.HORIZONTAL;
addComponent( button1, 0, 1, 2, 1 );
how on earth does the compiler know which component has which constraints.fill
assigned to it? I mean, how does it know that this constraints.fill = GridBagConstraints.BOTH;
refers to the textArea1 and this constraints.fill = GridBagConstraints.HORIZONTAL;
to button1? That applies of course to the other type of constraints assigned to other components
3)import java.awt.Component;
Method addComponent()
private void addComponent( Component component,
int row, int column, int width, int height )
{
constraints.gridx = column; // set gridx
constraints.gridy = row; // set gridy
constraints.gridwidth = width; // set gridwidth
constraints.gridheight = height; // set gridheight
layout.setConstraints( component, constraints ); // set constraints
add( component ); // add component
} // end method addComponent
takes 5 parameters. A Component components
is among them and I am not entirely sure as to why. Let's se if I get this right. Every JAVA component, like a JtTextField
, JButton
, JComboBox
etc is also a Component object. Since the parameters passed to the method are of a different type ( JtTextField, JButton, JComboBox
) the safest way to make sure that the method can receive any of the above types is if it accepts a parameter of type Component
(since all the components in questions as said are afterall all Components ). Correct?
But I wonder, if JtTextField
, JButton
, JComboBox
are all objects of type Component
, they are also objects of type JComponent
, aren't they? So the first parameter that the addComponent()
method receives could in fact be of type JComponent
as opposed to Component
?