Home > Back-end >  How can I call the mouse right click event in SwingGui?
How can I call the mouse right click event in SwingGui?

Time:06-19

I have a table. If I right-click I got a JPopUpMenu but before the pop-up I want to select the row where the right-click event is done. Here is what I've tried.

path_tbl.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            System.out.println(e.getPoint());

            Point point = e.getPoint();
            int selectedRow = path_tbl.rowAtPoint(point);
            path_tbl.setRowSelectionInterval(selectedRow, selectedRow);
        }
    });

In that event, I cannot get any output from the console when I right-click. However, when I left-click, points are printed to the console.

java.awt.Point[x=105,y=76]

So, this event only works when I left-click. How can I make this event work with right-click?

CodePudding user response:

Since you want custom mouse behavior, you should not use enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public final class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            DefaultTableModel model = new DefaultTableModel(0, 10);
            for (int row = 0; row < 10; row  ) {
                Vector data = new Vector(10);
                for (int col = 0; col < 10; col  ) {

                    String value = row   "x"   ((char) (col   'A'));
                    data.add(value);

                }
                model.addRow(data);
            }

            JPopupMenu menu = new JPopupMenu();
            menu.add("Hello");
            menu.add("This looks interesting");
            menu.add("I like bi-planes");

            JTable table = new JTable(model);
            table.setComponentPopupMenu(menu);
            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (SwingUtilities.isRightMouseButton(e)) {
                        Point point = e.getPoint();
                        int row = table.rowAtPoint(point);

                        if (!table.isRowSelected(row)) {
                            table.setRowSelectionInterval(row, row);
                        }
                    }
                }

            });
            add(new JScrollPane(table));
        }

    }
}
  • Related