In Java Swing, specifically with JTable, you can handle events when a row is selected and clicked using a ListSelectionListener combined with a MouseListener. Here's how you can achieve this:
Create a JTable and Add it to a JScrollPane:
First, create a JTable and add it to a JScrollPane for displaying data:
import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("JTable Selected Row Click Event"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Sample data for the table Object[][] data = { {"John", "Doe", 28}, {"Jane", "Smith", 32}, {"Tom", "Brown", 25} }; // Column names String[] columnNames = {"First Name", "Last Name", "Age"}; // Create JTable JTable table = new JTable(data, columnNames); JScrollPane scrollPane = new JScrollPane(table); // Add the scroll pane to the frame frame.add(scrollPane); // Add list selection listener table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { int selectedRow = table.getSelectedRow(); if (selectedRow != -1) { System.out.println("Selected Row: " + selectedRow); } } } }); // Add mouse click listener table.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { // Double-click detected int selectedRow = table.getSelectedRow(); if (selectedRow != -1) { System.out.println("Double-clicked Row: " + selectedRow); // Example: Get data from the table String firstName = (String) table.getValueAt(selectedRow, 0); String lastName = (String) table.getValueAt(selectedRow, 1); int age = (int) table.getValueAt(selectedRow, 2); System.out.println("Selected Data: " + firstName + " " + lastName + ", Age: " + age); } } } }); // Set frame size and visibility frame.setSize(400, 300); frame.setVisible(true); } } Handle Selection Events with ListSelectionListener:
Use a ListSelectionListener to detect changes in selection. This listener will trigger whenever the selection in the table changes, allowing you to react to row selections.
Handle Click Events with MouseListener:
Use a MouseListener to detect mouse events on the table. In this example, a double-click (getClickCount() == 2) on a row triggers an action. You can customize this to handle single-clicks (getClickCount() == 1) or other mouse events as needed.
JTable. It triggers when the user selects a different row.JTable. In this example, it's used to detect double-clicks on rows for more detailed actions.data and columnNames) to demonstrate row selection and data retrieval.JTable in this case) are updated on the Event Dispatch Thread (EDT) for thread safety.MouseListener) to handle different types of mouse events based on your application's requirements.getValueAt(row, column) to retrieve data from the selected row in the JTable.By combining ListSelectionListener and MouseListener, you can effectively handle row selection and click events in a JTable within your Java Swing application. Adjust the logic inside these listeners to suit your specific application needs, such as updating UI components or performing actions based on selected data.
Java JTable Selected Row Click Event: Using ListSelectionListener Description: Registers a ListSelectionListener to detect row selection changes.
import javax.swing.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.*; public class MyTablePanel extends JPanel { private JTable table; public MyTablePanel() { String[] columnNames = {"Column 1", "Column 2", "Column 3"}; Object[][] data = { {"Data 1", "Data 2", "Data 3"}, {"Data 4", "Data 5", "Data 6"} }; table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); ListSelectionModel selectionModel = table.getSelectionModel(); selectionModel.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { int selectedRow = table.getSelectedRow(); System.out.println("Selected Row: " + selectedRow); } } }); } public static void main(String[] args) { JFrame frame = new JFrame("JTable Selected Row Click Event"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyTablePanel panel = new MyTablePanel(); frame.add(panel); frame.pack(); frame.setVisible(true); } } ListSelectionListener to JTable to handle row selection events and prints the selected row index.Java JTable Selected Row Click Event: Using MouseListener Description: Implements MouseListener to detect clicks on selected rows.
import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class MyTablePanel extends JPanel { private JTable table; public MyTablePanel() { String[] columnNames = {"Column 1", "Column 2", "Column 3"}; Object[][] data = { {"Data 1", "Data 2", "Data 3"}, {"Data 4", "Data 5", "Data 6"} }; table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 1) { JTable target = (JTable) e.getSource(); int row = target.getSelectedRow(); System.out.println("Selected Row: " + row); } } }); } public static void main(String[] args) { JFrame frame = new JFrame("JTable Selected Row Click Event"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyTablePanel panel = new MyTablePanel(); frame.add(panel); frame.pack(); frame.setVisible(true); } } MouseListener to handle single clicks on selected rows in JTable.Java JTable Selected Row Click Event: Using SelectionModel Description: Retrieves selected row using SelectionModel directly.
import javax.swing.*; import java.awt.*; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class MyTablePanel extends JPanel { private JTable table; public MyTablePanel() { String[] columnNames = {"Column 1", "Column 2", "Column 3"}; Object[][] data = { {"Data 1", "Data 2", "Data 3"}, {"Data 4", "Data 5", "Data 6"} }; table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); ListSelectionModel selectionModel = table.getSelectionModel(); selectionModel.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (!e.getValueIsAdjusting()) { int selectedRow = table.getSelectedRow(); System.out.println("Selected Row: " + selectedRow); } } }); } public static void main(String[] args) { JFrame frame = new JFrame("JTable Selected Row Click Event"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyTablePanel panel = new MyTablePanel(); frame.add(panel); frame.pack(); frame.setVisible(true); } } SelectionModel to handle row selection events in JTable and prints the selected row index.Java JTable Selected Row Click Event: Using AbstractAction Description: Implements action with AbstractAction on row selection.
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class MyTablePanel extends JPanel { private JTable table; public MyTablePanel() { String[] columnNames = {"Column 1", "Column 2", "Column 3"}; Object[][] data = { {"Data 1", "Data 2", "Data 3"}, {"Data 4", "Data 5", "Data 6"} }; table = new JTable(data, columnNames); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); Action selectRowAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { int selectedRow = table.getSelectedRow(); System.out.println("Selected Row: " + selectedRow); } }; table.getActionMap().put("selectRow", selectRowAction); table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put( KeyStroke.getKeyStroke("ENTER"), "selectRow" ); } public static void main(String[] args) { JFrame frame = new JFrame("JTable Selected Row Click Event"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyTablePanel panel = new MyTablePanel(); frame.add(panel); frame.pack(); frame.setVisible(true); } } AbstractAction to handle row selection events triggered by Enter key or action mapping in JTable.Java JTable Selected Row Click Event: Using TableModelListener Description: Implements TableModelListener to detect changes in table model.
import javax.swing.*; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.util.Vector; public class MyTablePanel extends JPanel { private JTable table; public MyTablePanel() { String[] columnNames = {"Column 1", "Column 2", "Column 3"}; Vector<Vector<String>> data = new Vector<>(); Vector<String> row1 = new Vector<>(Arrays.asList("Data 1", "Data 2", "Data 3")); Vector<String> row2 = new Vector<>(Arrays.asList("Data 4", "Data 5", "Data 6")); data.add(row1); data.add(row2); DefaultTableModel model = new DefaultTableModel(data, new Vector<>(Arrays.asList(columnNames))); table = new JTable(model); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); model.addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e) { if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == TableModelEvent.ALL_COLUMNS) { int selectedRow = table.getSelectedRow(); System.out.println("Selected Row: " + selectedRow); } } }); } public static void main(String[] args) { JFrame frame = new JFrame("JTable Selected Row Click Event"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyTablePanel panel = new MyTablePanel(); frame.add(panel); frame.pack(); frame.setVisible(true); } } TableModelListener to monitor updates in table model and print selected row index in JTable.Java JTable Selected Row Click Event: Using TableRowSorter Description: Applies TableRowSorter to detect selection changes.
onclick telethon lit-html runonce window-handles touchpad storage nullable java-5 out-of-memory