-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Determine the table from the event's source in a Java Swing application
- Loading branch information
1 parent
179f3c5
commit 8d1f8cc
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/main/java/playground/swing/TableEventSourceExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |