Solved

JTable, how to get mouse click event here?

Posted on 2007-03-19
5
5,697 Views
Last Modified: 2012-05-05
Hello experts,

I am writing Swing application using JTable.. exactly what I am doing is based on TableDemo.java that exists in http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

But then I modified the code inside TableModel.java by taking out the inner class of TableModel.java into a separate class, i called it MyTableModel.java.

So the result is MyTableModel.java & TableModel.java. MyTableModel.java will accept an array list as data source. And after I have MyTableModel.java, I would be able to instantiate TableModel.java and passed an instance of MyTableModel during construction time.

Now it's working properly. My question is, I would like to enhance existing code to accept any click event within the table cells. Where should I put Listener class at? And what interface should be implemented by Listener class?
0
Comment
Question by:isuhendro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 18754184
Here's how you do it, this one waits for a double click but you can customize it easily for a single click:
http://www.rgagnon.com/javadetails/java-0336.html
0
 
LVL 15

Accepted Solution

by:
Javatm earned 125 total points
ID: 18754190
> Where should I put Listener class at? And what interface should be implemented by Listener class?

On your table:
YourTable.addMouseListener(new MouseAdapter(){
     public void mouseClicked(MouseEvent e){
      if (e.getClickCount() == 2){
         // Do your stuff
      }
    }
} );
0
 

Author Comment

by:isuhendro
ID: 18754335
hi wizard,

thanks for your clue... i am trying your suggestion and able to trap the event.. but if i am not wrong this event is a kind of event that triggered by JTable object, by then can I trap the cell information as well?

I am trying using e.getX() and e.getY().. but the result of X,Y is just cell position in pixel unit, not the cell matrix information.

0
 

Author Comment

by:isuhendro
ID: 18754694
hi wizard,

i got to find these below to complete the solution on getting cell matrix information

table.getSelectedRow();
table.getSelectedCount();

Thank you.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 18760838
I'm glad I was able to help
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question