• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

MouseEvent on JTable

 
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.
 
Rancher
Posts: 1965
15
Android Java ME Eclipse IDE Java
  • 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?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use java.awt.Robot.

db
 
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()
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!