From 8d1f8cc54cb4fc783d08446a4a74ae637dd4205f Mon Sep 17 00:00:00 2001 From: RalfBarkow Date: Sun, 9 Jun 2024 07:11:50 +0200 Subject: [PATCH] test: Table Event Source Example Determine the table from the event's source in a Java Swing application --- .../swing/TableEventSourceExample.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/main/java/playground/swing/TableEventSourceExample.java diff --git a/src/main/java/playground/swing/TableEventSourceExample.java b/src/main/java/playground/swing/TableEventSourceExample.java new file mode 100644 index 00000000..287344a4 --- /dev/null +++ b/src/main/java/playground/swing/TableEventSourceExample.java @@ -0,0 +1,36 @@ +package playground.swing; + +import javax.swing.*; +import javax.swing.table.DefaultTableModel; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; + +public class TableEventSourceExample { + + public static void main(String[] args) { + JFrame frame = new JFrame("Table Event Source Example"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + DefaultTableModel model = new DefaultTableModel(new Object[]{"Column1", "Column2"}, 0); + model.addRow(new Object[]{"Row1-Column1", "Row1-Column2"}); + model.addRow(new Object[]{"Row2-Column1", "Row2-Column2"}); + + JTable table = new JTable(model); + + table.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + Object source = e.getSource(); + if (source instanceof JTable) { + JTable clickedTable = (JTable) source; + System.out.println("Clicked on table: " + clickedTable); + } + } + }); + + frame.add(new JScrollPane(table)); + frame.setSize(300, 200); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } +}