Skip to content

Commit

Permalink
test: Table Event Source Example
Browse files Browse the repository at this point in the history
Determine the table from the event's source in a Java Swing application
  • Loading branch information
RalfBarkow committed Jun 9, 2024
1 parent 179f3c5 commit 8d1f8cc
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/playground/swing/TableEventSourceExample.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 8d1f8cc

Please sign in to comment.