import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AppSwing extends JFrame
{
	private JLabel lblNome;
	private JTextField txtNome;
	private JButton btnInserir;
	private JButton btnRemover;
	private JList listNomes;
	private JScrollPane paneNomes;
	private String nomes[] = {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
	private int numNomes = 0;

	class EventoBotaoInserir implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			if (txtNome.getText() != "")
			{
				nomes[numNomes++] = txtNome.getText();
				txtNome.setText("");
				paneNomes.updateUI();
			}
		}
	}

	class EventoBotaoRemover implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			int i = listNomes.getSelectedIndex();
			if ( nomes[i] != " " )
			{
				nomes[i] = nomes[--numNomes];
				nomes[numNomes] = " ";
				paneNomes.updateUI();
			}
		}
	}

	class EventoClicarLista extends MouseAdapter
	{
		public void mouseClicked(MouseEvent e)
		{
			if (e.getClickCount() == 2)
			{
				int i = listNomes.getSelectedIndex();
				if ( nomes[i] != " " )
				{
					nomes[i] = nomes[--numNomes];
					nomes[numNomes] = " ";
					paneNomes.updateUI();
				}
			}
		}
	}


	public AppSwing(String tituloJanela)
	{
		//instancia a janela e define o seu titulo
		super(tituloJanela);
		setLayout(null);

		//inclui nesta janela os componentes necessarios
		this.incluiBotaoInserir();
		this.incluiBotaoRemover();
		this.incluiCaixaDeTexto();
		this.incluiListaDeNomes();
	}

	void incluiBotaoInserir()
	{
		btnInserir = new JButton("Inserir");
		btnInserir.setBounds(30, 15, 100, 25);
		btnInserir.addActionListener( new EventoBotaoInserir() );
		add(btnInserir);
	}

	void incluiBotaoRemover()
	{
		btnRemover = new JButton("Remover");
		btnRemover.setBounds(140, 15, 100, 25);
		btnRemover.addActionListener( new EventoBotaoRemover() );
		add(btnRemover);
	}

	void incluiCaixaDeTexto()
	{
		txtNome = new JTextField();
		txtNome.setBounds(10, 50, 240, 20);
		add(txtNome);
	}

	void incluiListaDeNomes()
	{
		//rotulo da lista
		lblNome = new JLabel("Nomes");
		lblNome.setBounds(110, 70, 50, 20);
		add(lblNome);

		//lista que contem uma String com os possiveis nomes
		listNomes = new JList(nomes);
		listNomes.setFixedCellWidth(230);
		listNomes.setVisibleRowCount(12);
		listNomes.addMouseListener( new EventoClicarLista() );
		paneNomes = new JScrollPane(listNomes);
		paneNomes.setBounds(10, 100, 240, 200);
		add(paneNomes);
	}
	
	public static void main(String[] args)
	{
		//chama o construtor da aplicacao, que instancia seus componentes o os exibe na tela
		AppSwing janela = new AppSwing("Lista de presenca");
	    janela.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	    janela.setSize(260,340);
	    janela.setVisible( true );
	}
}
