Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Mouse click in JTable

843806Oct 15 2008 — edited Oct 17 2008
Hi All,
I want to force mouse click when right clicking on a JTable. I mean when the right click is done in the mouse, the left click should be forced. I have a JTable with some values in it. I want to select the particular column in the table when right click mouse action. How can I do it?
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TableDemo extends JFrame
{
    private JTable table;
    private JScrollPane jsp;
    private JPanel jp;

    public TableDemo()
    {
        initComponents();
    }

    private void initComponents()
    {
        jp = new JPanel();
        String columnNames[] = { "Column 1", "Column 2", "Column 3" };
        String dataValues[][] =
        {
            { "12", "234", "67" },
            { "-123", "43", "853" },
            { "93", "89.2", "109" },
            { "279", "9033", "3092" }
        };


        table = new JTable(dataValues, columnNames);
        jsp = new JScrollPane(table);
        jsp.setViewportView(table);
        jp.add(jsp);

        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                if(evt.getButton() == 3) {
                    System.out.println("Right Click........");
                    // Mouse left click should be forced here to select the row.

                }
                if(evt.getButton() == 1) {
                    System.out.println("Mouse LEFT CLICK FORCED.....");
                }
            }
         });

        getContentPane().add(jp);
        pack();
        setVisible(true);
    }
    public static void main(String args[])
    {
        new TableDemo();
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 14 2008
Added on Oct 15 2008
8 comments
1,073 views