/*
 * 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.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

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

		final JRadioButton radioButton1 = new JRadioButton("Button 1");
		radioButton1.setMnemonic(KeyEvent.VK_A);// Alt와 함께 누른다.
		radioButton1.setActionCommand("btn1");
		radioButton1.setSelected(true);

		final JRadioButton radioButton2 = new JRadioButton("Button 2");
		radioButton2.setMnemonic(KeyEvent.VK_B);
		radioButton2.setActionCommand("btn2");

		// 레이아웃에 라디오 버튼을 추가
		add(radioButton1);
		add(radioButton2);

		// 버튼에 액션 리스너를 연결한다.
		final ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				switch (actionEvent.getActionCommand()){
					case "btn1":
						System.out.println("btn1 is selected.");
						break;
					case "btn2":
						System.out.println("btn2 is selected.");
						break;
					default:
						System.out.println("oops!");
						break;
				}
			}
		};
		radioButton1.addActionListener(actionListener);
		radioButton2.addActionListener(actionListener);

		// 라디오 버튼을 그룹으로 묶어준다.
		final ButtonGroup group = new ButtonGroup();
		group.add(radioButton1);
		group.add(radioButton2);
	}

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

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


	}


}
