package listener;

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

public class ComboBoxListener extends JFrame {

	private static final long serialVersionUID = 1L;

	public static void main(String args[]) {
		new ComboBoxListener();
	}
	
	public ComboBoxListener() {
		super("TooltipInSwing");
		setSize(400, 300);
		this.getContentPane().setLayout(new FlowLayout());
		this.getContentPane().setBackground(Color.WHITE);
		JLabel lblOne = new JLabel("Watch the changes here -->");
		final JLabel lblTwo = new JLabel("Default label");
		lblTwo.setForeground(new Color(0, 200, 50));
		final JComboBox<String> cboOne = 
				new JComboBox<String>(new String[] 
						{ " ", "one", "two", "three" });

		this.getContentPane().add(lblOne);
		this.getContentPane().add(lblTwo);
		this.getContentPane().add(cboOne);

		cboOne.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// setting custom text to JLabel
				if (cboOne.getSelectedItem().toString().equals("one"))
					lblTwo.setText("label one ---");
				else if (cboOne.getSelectedItem().toString().equals("two"))
					lblTwo.setText("label two ---");
				else if (cboOne.getSelectedItem().toString().equals("three"))
					lblTwo.setText("label three ---");
				else
					lblTwo.setText("----");

				// or Compact version for setting JcomboBox selected item 
				// to JLabel text

				System.out.println(cboOne.getSelectedItem().toString());
				System.out.println(cboOne.getSelectedIndex());
			}
		});

		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setVisible(true);
	}


}

