Computer >> Máy Tính >  >> Lập trình >> Java

Làm thế nào chúng ta có thể phát hiện các sự kiện nhấp đúp của một hàng JTable trong Java?

A JTable là một lớp con của JComponent để hiển thị các cấu trúc dữ liệu phức tạp. JTable có thể tuân theo mẫu thiết kế Model View Controller (MVC) để hiển thị dữ liệu trong các hàng và cột. JTable có thể tạo TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener RowSorterListener các giao diện. Chúng tôi có thể phát hiện các sự kiện nhấp đúp của JTable bằng cách sử dụng MouseAdapter lớp hoặc MouseListener giao diện. Chúng tôi có thể đặt getClickCount () giá trị thành '2' của MouseEvent lớp để phát hiện các sự kiện nhấp đúp của JTable.

Ví dụ

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public final class DoublClickJTableRowTest extends JFrame {
   private JTable table;
   private JScrollPane scrollPane;
   public DoublClickJTableRowTest() {
      setTitle("DoublClickJTableRow Test");
      String[] columnNames = {"First Name", "Last Name"};
      Object[][] data = {{"Raja", "Ramesh"}, {"Adithya", "Sai"}, {"Vineet", "Kumar"}, {"Krishna", "Kasyap"}};
      table = new JTable(data, columnNames) {
         public boolean editCellAt(int row, int column, java.util.EventObject e) {
            return false;
        }
      };
      table.setFocusable(false);
      table.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent me) {
            if (me.getClickCount() == 2) {     // to detect doble click events
               JTable target = (JTable)me.getSource();
               int row = target.getSelectedRow(); // select a row
               int column = target.getSelectedColumn(); // select a column
              JOptionPane.showMessageDialog(null, table.getValueAt(row, column)); // get the value of a row and column.
            }
         }
      });
      scrollPane= new JScrollPane(table);
      add(scrollPane);
      setSize(375, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLocationRelativeTo(null);
      setVisible(true);
   }
   public static void main(String[] args) {
      new DoublClickJTableRowTest();
   }
}

Đầu ra

Làm thế nào chúng ta có thể phát hiện các sự kiện nhấp đúp của một hàng JTable trong Java?