Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseEvent on JTable

 
Susmitha Metlapalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use java.awt.Robot.

db
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>... rather than manually clicking on JTable for selecting a specific row...

table.changeSelection(....);
 
Susmitha Metlapalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic