/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test2;

/**
 *
 * @author Mahmoud Mahrous
 */
import javax.swing.*;
import java.lang.*;
import java.awt.event.*;

public class GUITest extends JFrame {

    public JButton b1 = new JButton("Add");
    public JButton b2 = new JButton("Exit..");
    public JTextField t1 = new JTextField(15);
    public JPanel p1 = new JPanel();

    public void addComponentsAndListeners() {

        this.getContentPane().add(p1);
        p1.add(new JButton("Test.."));
        p1.add(b1);
        p1.add(b2);
        pack();
        b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (e.getActionCommand().equals("Add")) {
                    p1.add(t1);
                    System.out.println("Action ->" + e.getActionCommand());
                    b1.setText("Remove");
                    b1.setActionCommand("Remove");
                    pack();
                } else if (e.getActionCommand().equals("Remove")) {
                    p1.remove(t1);
                    System.out.println("Action ->" + e.getActionCommand());
                    b1.setText("Add");
                    b1.setActionCommand("Add");
                    pack();
                }
                validate();
            }
        });

        b2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    public JPanel getPanel() {
        return p1;
    }

    public static void main(String[] args) {
        GUITest myFrame = new GUITest();
        myFrame.addComponentsAndListeners();
        myFrame.setVisible(true);
    }
}
