Solved

JTable, how to get mouse click event here?

Posted on 2007-03-19
5
5,683 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now