package exercicio15;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
 *
 * @author Gerlandio da Silva Lucena(Webforcraft)
 */
public class Exercicio15 extends JFrame implements ActionListener, ItemListener{
 
    JLabel lbl1;
    JTextField txt1, txt2, txt3;
    JComboBox combo;
    JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8;
    public static void main(String[] args) 
    {
       JFrame jcombo = new Exercicio15();
       jcombo.setUndecorated(true);
       jcombo.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
       jcombo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       jcombo.setVisible(true);
    }
     
    Exercicio15()
    {
    setTitle("Combo box em Java");
    setBounds(200,50,400,170);
    getContentPane().setBackground(new Color(145,233, 221));
    lbl1 = new JLabel("Conteudo");
    lbl1.setForeground(Color.black);
    lbl1.setFont(new Font("Arial", Font.BOLD,15));
    btn1 = new JButton("Mostra Texto"); 
    btn1.addActionListener(this); 
    btn2 = new JButton("Mostra Indice");
    btn2.addActionListener(this);
    btn3 = new JButton("Adiciona Item");
    btn3.addActionListener(this);
    btn4 = new JButton("Remove itens");
    btn4.addActionListener(this);
    btn5 = new JButton("Remove Todos");
    btn5.addActionListener(this);
    btn6 = new JButton("Quant. Itens");
    btn6.addActionListener(this);
    txt1 = new JTextField();
    txt2 = new JTextField();
    String[] generos = {"Ficção", "Ciência", "Romance","Epico", "Aventura"};
    combo = new JComboBox(generos);
    combo.addItemListener(this);
    getContentPane().setLayout(new GridLayout(5,2));
    getContentPane().add(lbl1);
    getContentPane().add(combo);
    getContentPane().add(btn1);getContentPane().add(btn4);
    getContentPane().add(btn2);getContentPane().add(btn5);
    getContentPane().add(btn3);getContentPane().add(txt1);
    getContentPane().add(btn6);getContentPane().add(txt2);
         
     
     
    }
     
    public void actionPerformed(ActionEvent e)
     {
         if(e.getSource()==btn1)
         {
         lbl1.setText("Texto : "+combo.getSelectedItem());
         }
         if(e.getSource()== btn2)
         {
          lbl1.setText("Indice : "+ combo.getSelectedIndex());         
         }
         if(e.getSource()==btn3 && txt1.getText().length()!=0)
         {
         combo.addItem(txt1.getText());
         txt1.setText("");
         }
         if(e.getSource() == btn4)
         {
         combo.removeItemAt(combo.getSelectedIndex());
         }
         if(e.getSource() == btn5)
         {
         combo.removeAllItems();
         }
         if(e.getSource()== btn6)
         {
         txt2.setText(""+combo.getItemCount());
         }
          
     }
    public void itemStateChanged(ItemEvent e)
    {
     txt1.setText(""+combo.getSelectedItem());
    }
}