package lezione_10;

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

public class EsempioComboBox implements ActionListener {
	
	JLabel etichetta;
	
	public EsempioComboBox(JLabel etichetta) {
		this.etichetta=etichetta;
	}
	
	public static void main(String[] args) {
		
		JFrame f = new JFrame("Questa è la mia finestra");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel pannello = new JPanel();
		f.setContentPane(pannello);

		JLabel etichetta = new JLabel("ETICHETTA");
		
		String[] lista={"rosso","verde","blue"};
		
		JComboBox colori=new JComboBox(lista);
		colori.setSelectedIndex(2);
		colori.addActionListener(new EsempioComboBox(etichetta));
		
		pannello.add(colori);
		pannello.add(etichetta);
		
		f.pack();
		f.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox) e.getSource();
        String colore = (String) cb.getSelectedItem();
        Color[] lista={Color.RED,Color.GREEN,Color.BLUE};
        int indice = cb.getSelectedIndex();
        etichetta.setText(colore);
        etichetta.setForeground(lista[indice]);
    }
}
