package lezione_10;

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

public class EsempioComboBox2 implements ActionListener{
	JTextField testo;
	JComboBox combo;
	
	public EsempioComboBox2(JTextField testo,JComboBox combo) {
		this.testo=testo;
		this.combo=combo;
	}
	
	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);

		JTextField testo = new JTextField(20);
		
		JButton aggiungi = new JButton("Aggiungi");

		String[] lista={};
		JComboBox combo=new JComboBox(lista);

		aggiungi.addActionListener(new EsempioComboBox2(testo,combo));
		
		pannello.add(combo);
		pannello.add(testo);
		pannello.add(aggiungi);
		
		f.pack();
		f.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
        int n = combo.getItemCount();
        combo.insertItemAt(testo.getText(), n);
    }
}
