GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes MouseEvent on JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseEvent on JTable" Watch "MouseEvent on JTable" New topic
Author

MouseEvent on JTable

Susmitha Metlapalli
Ranch Hand

Joined: May 16, 2007
Posts: 45
Hi,

I have a JTable with data. I want to generate a single/double click mouse event programatically using java (MouseEvent me = new MouseEvent(source,id,when,x,y,clickcount,popiptrigger) rather than manually clicking on JTable for selecting a specific row...

Is it possible?

In the following program i used setRowSelectionInterval(0,0) for selecting a row programatically. I also have a mouse listener for JTable. But i dont want to use both of these.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import javax.swing.table.*;

public class TestJTable
{
public static void main(String args[])
{
TestJTable1 tj = new TestJTable1();
tj.setSize(800,800);
}
}

class TestJTable1 extends JFrame
{
public TestJTable1()
{
JTable jtb = new JTable();

setTitle("TestJTable1");
setVisible(true);

DefaultTableModel model = new DefaultTableModel(10, 10);

for (int i = 0; i < model.getRowCount(); i++)
{
for (int j = 0; j < model.getColumnCount(); j++)
{
model.setValueAt(new Integer((i + 1) * (j + 1)), i, j);

jtb.setModel(model);

Container contentPane = getContentPane();
contentPane.add(new JScrollPane(jtb), "Center");

jtb.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
System.out.println(me);
}
});

}
}
jtb.setRowSelectionInterval(0,0);
}
}


Thanks in advance,
Susmitha.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

MouseEvent me = new MouseEvent(source,id,when,x,y,clickcount,popiptrigger), will simply create the MouseEvent object, but how can you handle the event without the listener?


Swastik
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4572
    
    5

Use java.awt.Robot.

db


luck, db
There are no new questions, but there may be new answers.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
>... rather than manually clicking on JTable for selecting a specific row...

table.changeSelection(....);
Susmitha Metlapalli
Ranch Hand

Joined: May 16, 2007
Posts: 45
Thanks for the replies.

I have to generate mouse double click event on specific cell of JTable programatically...

I cannot use java.awt.Robot, because i should not make this JFrame visible on the screen... I have to say setVisible(false) at later time...

Please help...

Thanks in advance,
Susmitha.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I have to generate mouse double click event on specific cell of JTable programatically...

you need to explain 'why' you need to generate a mouse double click event.

normally you would do something like this



instead of the above you could do this

and instead of trying to generate a mouse double-click event, you just call doSomething()
 
GeeCON Prague 2014
 
subject: MouseEvent on JTable