/**
 * Material para as aulas 34 a 36 do Curso Técnico em Informática
 * do Colégio Estadual do Paraná
 * Prof. André Aparecido da Silva 
 * Disponível no site http://www.oxnar.com.br/2017/3ati
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

public class OxnarExercicioJList02 extends JFrame  implements ActionListener
{
		
		DefaultListModel 	Itens_de_Produtos 	= new DefaultListModel();
		JList 			 	Lista_de_Produtos	= new JList(Itens_de_Produtos );
		JScrollPane			Painel_Rolante		= new JScrollPane(Lista_de_Produtos);
		JTextField 			tf_Produto;
		JButton 			bt_Adicionar, bt_RemoveItem, bt_Limpar_Lista;  
		JPanel Painel_Norte, Painel_Centro, Painel_Sul; 
        

    	public OxnarExercicioJList02() 
    	{
    			getContentPane().setLayout(new BorderLayout());
    			getContentPane().add("North", Painel_Norte    = new JPanel (new FlowLayout(FlowLayout.LEFT)));
    			
    			Painel_Norte.add(tf_Produto     = new JTextField(15));
    			Painel_Norte.add(bt_Adicionar   = new JButton("ADICIONAR ITEM"));
    			getContentPane().add("Center", Painel_Centro	 	= new JPanel (new FlowLayout(FlowLayout.CENTER)));
    			Painel_Centro.add(Painel_Rolante); 
    			Lista_de_Produtos.setVisibleRowCount(15);	
    			getContentPane().add("South",  Painel_Sul			= new JPanel (new FlowLayout(FlowLayout.CENTER)));
    			Painel_Sul.add(bt_RemoveItem	= new JButton("REMOVER ITENS"));
    			Painel_Sul.add(bt_Limpar_Lista  = new JButton("LIMPAR LISTA"));
    			setTitle("JList - EXERCICIO 02 DA AULA DE INTERFACES 08");
					
				setVisible(true);
				//setSize(450, 400);
				pack(); 	
				setResizable(false);	
				bt_Adicionar.addActionListener(this);
				bt_RemoveItem.addActionListener(this);
				bt_Limpar_Lista.addActionListener(this);		

    	}
    	/////////////////////////////////////////
  
        ////////////////////////////////////////////////
    	public static void main (String args [])
    			{new OxnarExercicioJList02();}
    	public void actionPerformed (ActionEvent e)
    	{
    			if(e.getSource()==bt_Adicionar)
    			{
    					if(tf_Produto.getText().equals(""))
    					{
    							JOptionPane.showMessageDialog(null, "Você precisa digitar um item");
    					}	
    					else
    					{
    							Itens_de_Produtos.addElement(tf_Produto.getText().toUpperCase());
    							tf_Produto.setText("");
    					}		
    					
    			}	
    			if(e.getSource()==bt_RemoveItem)
    					{
    						
    						Itens_de_Produtos.remove(Lista_de_Produtos.getAnchorSelectionIndex());
    					}	
    			if(e.getSource()==bt_Limpar_Lista) 
    					{Itens_de_Produtos.removeAllElements();}		    		
    	}        
}