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

package com.elex_project.examples.components.button;

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

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

		final JCheckBox checkBox1 = new JCheckBox("Button 1");
		checkBox1.setMnemonic(KeyEvent.VK_A);// Alt와 함께 누른다.
		checkBox1.setSelected(true);

		final JCheckBox checkBox2 = new JCheckBox("Button 2");
		checkBox2.setMnemonic(KeyEvent.VK_B);

		// 레이아웃에 추가한다.
		add(checkBox1);
		add(checkBox2);

		// 아이템 리스너를 통해서 이벤트를 받는다.
		final ItemListener itemListener = new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent itemEvent) {
				ItemSelectable source = itemEvent.getItemSelectable();
				if(source==checkBox1){
					System.out.println(checkBox1.getText() + " is "
							+ (checkBox1.isSelected()?"selected.":"not selected."));
				} else if (source==checkBox2) {
					System.out.println(checkBox2.getText() + " is "
							+ (checkBox2.isSelected()?"selected.":"not selected."));
				}
			}
		};
		checkBox1.addItemListener(itemListener);
		checkBox2.addItemListener(itemListener);
	}

	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 CheckBoxExample());

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


	}

}
