package application;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class TableWithExternalProgram extends JFrame {
	private JTable table;
	private DefaultTableModel model;

	public TableWithExternalProgram() {
		setTitle("Tabelle mit externem Programm");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(400, 300);
		setLocationRelativeTo(null);

		// Erstellen der Tabelle
		String[] columnNames = { "Name", "Alter", "Beruf" };
		Object[][] data = { { "John", 25, "Ingenieur" }, { "Alice", 30, "Ärztin" }, { "Bob", 35, "Lehrer" } };

		model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);

		// Hinzufügen des MouseListeners für Zellklicks
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 1) {
					int row = table.rowAtPoint(e.getPoint());
					int column = table.columnAtPoint(e.getPoint());
					if (row >= 0 && column >= 0) {
						String value = (String) table.getValueAt(row, column);
						runExternalProgram(value);
					}
				}
			}
		});

		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane, BorderLayout.CENTER);
	}

	private void runExternalProgram(String value) {
		try {
			// Beispiel: Öffnen der Systemeinstellungen
			Runtime.getRuntime().exec("control.exe");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(() -> {
			TableWithExternalProgram frame = new TableWithExternalProgram();
			frame.setVisible(true);
		});
	}
}
