This article describes the operation of adding right-click event listening for Java to implement table. Share it for your reference, as follows:
Table monitoring steps
1 Add a listener to the table
2 Use the mouseClicked method of the MouseAdapter class
3 Through the getbutton method of the MouseEvent class object, it is determined that the mouse operation BUTTON1 is the left button BUTTON3 is the right button
import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.util.Vector;import javax.swing.*;import javax.swing.table.DefaultTableModel;public class TableSj extends JFrame{ JTable table; DefaultTableModel tableM; JScrollPane jsp; JPopupMenu jpm; Vector<String> name = new Vector<String>(); Vector<String> data = new Vector<String>(); public static void main(String[] args) { // TODO automatic generated method stub new TableSj(); } TableSj(){ name.add("name"); name.add("Age"); data.add("Zhang San"); data.add("19"); tableM = new DefaultTableModel(name,0); tableM.addRow(data); tableM.addRow(data); table = new JTable(tableM); jsp = new JScrollPane(table); table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e){ if (e.getButton() == MouseEvent.BUTTON3){ //Show jpm = new JPopupMenu(); //The rowAtPoint method of the table returns the line number where the coordinate is located, the parameter is the coordinate type, int i = table.rowAtPoint(e.getPoint()); jpm.add(i+""); jpm.show(table, e.getX(), e.getY()); } } }); this.add(jsp); this.setVisible(true); this.setSize(400, 400); this.setDefaultCloseOperation(EXIT_ON_CLOSE); }}For more Java-related content, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.