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

package com.elex_project.examples.components.textfield;

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

public class TextAreaExample extends JPanel {
	public TextAreaExample() {
		super(new BorderLayout());

		final JTextField textField = new JTextField();

		final JTextArea textArea = new JTextArea();
		textArea.setText("Hello\n");
		textArea.setEditable(true);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);

		add(textField, BorderLayout.NORTH);
		add(new JScrollPane(textArea), BorderLayout.CENTER);

		textField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				textArea.append(textField.getText() + "\n");
				textArea.setCaretPosition(textArea.getDocument().getLength());
				textField.setText(null);
			}
		});
	}

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

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


	}
}
