Solved

JTable, how to get mouse click event here?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

816 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