同一JFrame中点击JTable行后在另一JTable行打印文本的实现方法
Got it, let's walk through how to build this Swing functionality step by step. The core idea is to listen for row selection events on JTable2, then update JTable1's data model with your specified text whenever a row is clicked.
Step 1: Set Up Table Models
First, we'll use DefaultTableModel for both tables—it's perfect for dynamic data changes since it automatically syncs with the JTable display. We'll define JTable1 with 2 columns and JTable2 with 4 columns.
Step 2: Add a Selection Listener to JTable2
We'll attach a ListSelectionListener to JTable2's selection model. This listener triggers whenever the user selects a row, which is exactly the event we need to trigger the update to JTable1.
Step 3: Update JTable1's Data
When a row is selected in JTable2, we'll either add a new row to JTable1 (with "hi" in column 1 and "welcome" in column 2) or update an existing row—both options are included in the code below.
Full Working Code Example
import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableModel; import java.awt.*; public class TableInteractionDemo extends JFrame { private JTable table1; private JTable table2; private DefaultTableModel model1; private DefaultTableModel model2; public TableInteractionDemo() { // Frame setup setTitle("Table Click Interaction"); setSize(800, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(1, 2)); // Initialize JTable1 (2 columns) and its model model1 = new DefaultTableModel(new String[]{"Column 1", "Column 2"}, 0); table1 = new JTable(model1); add(new JScrollPane(table1)); // Initialize JTable2 (4 columns) and its model, plus sample rows model2 = new DefaultTableModel(new String[]{"Col A", "Col B", "Col C", "Col D"}, 0); model2.addRow(new Object[]{"Row 1 Data", "Sample", "Text", "Here"}); model2.addRow(new Object[]{"Row 2 Data", "More", "Test", "Content"}); table2 = new JTable(model2); add(new JScrollPane(table2)); // Add selection listener to JTable2 table2.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { // Avoid duplicate triggers when selection is adjusting if (!e.getValueIsAdjusting()) { int selectedRow = table2.getSelectedRow(); if (selectedRow != -1) { // Ensure a row is actually selected // Option 1: Add a new row to JTable1 every time model1.addRow(new Object[]{"hi", "welcome"}); // Option 2: Update the first row of JTable1 instead of adding new ones // if (model1.getRowCount() == 0) { // model1.addRow(new Object[]{"hi", "welcome"}); // } else { // model1.setValueAt("hi", 0, 0); // model1.setValueAt("welcome", 0, 1); // } } } } }); } public static void main(String[] args) { // Run on Swing's event dispatch thread for safety SwingUtilities.invokeLater(() -> { new TableInteractionDemo().setVisible(true); }); } }
Key Notes:
valueIsAdjustingCheck: This stops the listener from firing twice (once when selection starts, once when it finalizes)—we only want to act when the user's selection is complete.- Two Update Options: The code includes both adding new rows to JTable1 and updating the first row. Uncomment the option that matches your needs.
- JScrollPane: Always wrap JTables in a
JScrollPaneso users can scroll if there are more rows than fit in the window.
Just compile and run this code, and you'll see clicking any row in JTable2 populates JTable1 with your specified text.
内容的提问来源于stack exchange,提问作者Divya

