package example7_1; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRootPane; import javax.swing.JTextField; // This app demonstrates the Composite pattern used in Swing components public class SwingCompositesDemoApp extends JFrame { private JTextField msgField; public static void main(String[] args) { SwingCompositesDemoApp app = new SwingCompositesDemoApp(); app.initUI(); } // create the JFrame and UI components private void initUI() { ButtonHandler bh = new ButtonHandler(); setSize(500, 500); // Get the content pane - the container that holds other UI elements // JRootPane jrp = this.getRootPane(); // Container contentPane = jrp.getContentPane(); Container contentPane = this.getContentPane(); // the common shortcut contentPane.setBackground(Color.black); contentPane.setLayout(new FlowLayout(FlowLayout.CENTER) ); // Create a text field for messages msgField = new JTextField("messages go here"); msgField.setPreferredSize(new Dimension(300, 25)); contentPane.add(msgField); {// Create a JPanel inside the content pane JPanel outerPanel = new JPanel(); outerPanel.setPreferredSize(new Dimension(400, 400)); contentPane.add(outerPanel); outerPanel.setLayout(new FlowLayout(FlowLayout.LEFT) ); outerPanel.setBackground(Color.green); {// Create an upper JPanel inside the outer JPanel (a composite containing a composite) JPanel innerPanelUpper = new JPanel(); innerPanelUpper.setPreferredSize(new Dimension(300, 100)); outerPanel.add(innerPanelUpper); innerPanelUpper.setLayout(new FlowLayout(FlowLayout.LEFT) ); innerPanelUpper.setBackground(Color.blue); { // Put a JButton inside the upper JPanel JButton upperButton = new JButton("upper"); upperButton.setPreferredSize(new Dimension(250, 35)); upperButton.setActionCommand("upper"); innerPanelUpper.add(upperButton); upperButton.addActionListener( bh ); // "attach" to observer } } {// Create an lower JPanel inside the outer JPanel JPanel innerPanelLower = new JPanel(); innerPanelLower.setPreferredSize(new Dimension(300, 100)); outerPanel.add(innerPanelLower); innerPanelLower.setLayout(new FlowLayout(FlowLayout.LEFT) ); innerPanelLower.setBackground(Color.yellow); {// Put a JButton inside the lower JPanel JButton lowerButton = new JButton("lower"); lowerButton.setPreferredSize(new Dimension(250, 35)); lowerButton.setActionCommand("lower"); lowerButton.addActionListener( bh ); // "attach" to observer lowerButton.setLayout(new FlowLayout(FlowLayout.LEFT) ); innerPanelLower.add(lowerButton); JButton lower2 = new JButton("hey"); lower2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { msgField.setText("Hey!"); } }); lowerButton.add(lower2); // does a button include components? // The "paint" method is probably the place to look. // lowerButton.paint(new Graphics()); } } } pack(); setVisible(true); } // handler for button presses; private class ButtonHandler implements ActionListener { @Override public void actionPerformed(ActionEvent event) { String cmd = event.getActionCommand(); // display the name of the button that was pressed msgField.setText(String.format( cmd + " was pressed.")); } } }