Answer:

I put each column of three label/textfield pairs into a panel, then placed those panels in the content pane using BoxLayout with horizontal alignment. The picture shows how the visual display corresponds to the nested panels.

You could also have used panels to group label/textfield pairs into rows, then put those three panels into the content pane using vertical alignment.

Vertical and Horizontal

nested panels

 

The default FlowLayout is used within the smallest panels (illustrated in red). This means that the label and text field are associated with each other, but that the best use is made of space. If the user elongates the frame, the labels will be placed left of the text fields. If the frame is not very wide, the labels will be placed above the text fields (as shown here).

Here is some of the code for the above solution:


public class LayoutEg2 extends JFrame
{

  // Create each small panel and the components that go into it
  JLabel     lData1  = new JLabel("Data Item 1");
  JTextField txData1 = new JTextField( 7 );
  JPanel     panel1  = new JPanel();
  . . . . . .
  JLabel     lData6  = new JLabel("Data Item 6");
  JTextField txData6 = new JTextField( 7 );
  JPanel     panel6  = new JPanel();
  
  // Create the Left and Right panels
  JPanel  panelLeft  = new JPanel();
  JPanel  panelRight = new JPanel();
  
  public LayoutEg2()  
  { 
    // Add components to the six small panels.
    panel1.add( lData1 ); panel1.add( txData1 );
    . . . . . . .
    panel6.add( lData6 ); panel6.add( txData6 );

    // Add small panels to the Left panel.  
    panelLeft.setLayout( new BoxLayout( panelLeft, BoxLayout.Y_AXIS ) ); 
    panelLeft.add ( panel1); panelLeft.add( panel2); 
    panelLeft.add ( panel3); 
    
    // Add small panels to the Right panel.
    panelRight.setLayout( new BoxLayout( panelRight, BoxLayout.Y_AXIS ) ); 
    panelRight.add( panel4); panelRight.add( panel5); 
    panelRight.add( panel6); 
 
    // Set the layout of the content pane to horizontal,
    // and add the Left and Right panel.
    getContentPane().setLayout( new BoxLayout( getContentPane(), BoxLayout.X_AXIS ) ); 
    getContentPane().add( panelLeft );
    getContentPane().add( panelRight );

    . . . . . .
  }

QUESTION 11:

May buttons be placed into a panel?