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

package com.elex_project.examples.components.slider;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;

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

		final JLabel label = new JLabel();

		final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
		slider.setMajorTickSpacing(10);
		slider.setMinorTickSpacing(5);
		slider.setPaintTicks(true);
		slider.setPaintLabels(true);
		slider.setPaintTrack(true);

		// 개발자 정의 라벨
		Dictionary<Integer,JLabel> dict = new Hashtable<>();
		dict.put(0, new JLabel("Minimum"));
		dict.put(50, new JLabel("50"));
		dict.put(100, new JLabel("Maximum"));
		slider.setLabelTable(dict);

		label.setText(String.valueOf(slider.getValue()));


		slider.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent changeEvent) {
				JSlider eventSource = (JSlider) changeEvent.getSource();
				if (!eventSource.getValueIsAdjusting()){
					label.setText(String.valueOf(eventSource.getValue()));
				}
			}
		});

		this.add(label);
		this.add(slider);

	}

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

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


	}
}
