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();
}
}