package grafica;

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

public class EsempioComboBox2 implements ActionListener {
	
	JLabel etichetta;
	JComboBox<String> cb;
	
	public EsempioComboBox2(JLabel etichetta,JComboBox<String> cb) {
		this.etichetta=etichetta;
		this.cb=cb;
	}
	
	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<String> colori=new JComboBox<String>(lista);
		colori.setSelectedIndex(2);
		colori.addActionListener(new EsempioComboBox2(etichetta, colori));
		
		pannello.add(colori);
		pannello.add(etichetta);
		
		f.pack();
		f.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
        String colore = (String) cb.getSelectedItem();
        Color[] lista={Color.RED,Color.GREEN,Color.BLUE};
        int indice = cb.getSelectedIndex();
        etichetta.setText(colore);
        etichetta.setForeground(lista[indice]);
    }
}
