//Initialize an empty list of buttons.
buttonList = new Vector(10, 10);
//Create all the components.
addButton = new JButton("Add a button");
addButton.setActionCommand(ADD);
addButton.addActionListener(this);
removeButton = new JButton("Remove a button");
removeButton.setActionCommand(REMOVE);
removeButton.addActionListener(this);
buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(200, 75));
buttonPanel.addContainerListener(this);
display = new JTextArea();
display.setEditable(false);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(200, 75));
clearButton = new JButton("Clear text area");
clearButton.setActionCommand(CLEAR);
clearButton.addActionListener(this);
//Lay out the components.
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
JPanel contentPane = new JPanel();
contentPane.setLayout(gridbag);
c.fill = GridBagConstraints.BOTH; //Fill entire cell.
c.weighty = 1.0; //Button area and message area have equal height.
c.gridwidth = GridBagConstraints.REMAINDER; //end of row
gridbag.setConstraints(scrollPane, c);
contentPane.add(scrollPane);
c.weighty = 0.0;
gridbag.setConstraints(clearButton, c);
contentPane.add(clearButton);
c.weightx = 1.0; //Add/remove buttons have equal width.
c.gridwidth = 1; //NOT end of row
gridbag.setConstraints(addButton, c);
contentPane.add(addButton);
c.gridwidth = GridBagConstraints.REMAINDER; //end of row
gridbag.setConstraints(removeButton, c);
contentPane.add(removeButton);
c.weighty = 1.0; //Button area and message area have equal height.
gridbag.setConstraints(buttonPanel, c);
contentPane.add(buttonPanel);
setContentPane(contentPane);