/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package com.elex_project.examples.components.combobox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class ComboBoxExample extends JPanel {
	public ComboBoxExample() {
		super(new FlowLayout());

		final String[] options = {"A", "B", "C", "D"};
		final JComboBox<String> comboBox = new JComboBox<>(options);
		comboBox.setSelectedIndex(2);

		// 액션 리스너를 통해서 이벤트를 받는다.
		comboBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				JComboBox<String> component = (JComboBox<String>) actionEvent.getSource();
				String selectedItem = (String) component.getSelectedItem();
				System.out.println(selectedItem);
			}
		});

		// 레이아웃에 추가
		add(comboBox);

	}

	public static void main(String... args) {

		final JFrame jFrame = new JFrame();
		jFrame.setTitle("Example");
		jFrame.setSize(800, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setContentPane(new ComboBoxExample());

		SwingUtilities.invokeLater(() -> {
			jFrame.setVisible(true);
		});


	}

	class DummyIcon implements Icon{

		@Override
		public void paintIcon(Component component, Graphics graphics, int x, int y) {
			graphics.setColor(Color.DARK_GRAY);
			graphics.fillRect(x,y,x+getIconWidth(),y+getIconHeight());
		}

		@Override
		public int getIconWidth() {
			return 16;
		}

		@Override
		public int getIconHeight() {
			return 16;
		}
	}
}
