Solved

Another JTable question !

Posted on 2002-04-25
5
179 Views
Last Modified: 2011-10-03
Hi,
Could anyone please show me how to get back the row of a JTable when I
doubleclick my mouse on to the table ?
Thankx,
Bye.
0
Comment
Question by:kianh
5 Comments
 

Expert Comment

by:chhabra99
ID: 6968026
I think this will help you. This will give you the row number u have selected.

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
ListSelectionModel rowSM = table.getSelectionModel();
rowSM.addListSelectionListener(new ListSelectionListener() {
   public void valueChanged(ListSelectionEvent e) {
                    //Ignore extra messages.
                   if (e.getValueIsAdjusting()) return;

                    ListSelectionModel lsm = (ListSelectionModel)e.getSource();
                    if (lsm.isSelectionEmpty()) {
                        System.out.println("No rows are selected.");
                    } else {
                        int selectedRow = lsm.getMinSelectionIndex();
                        System.out.println("Row " + selectedRow
                                           + " is now selected.");
                    }
                }
            });
}
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6968091
You'll need to use a MouseListener to determine which row is d-clicked on.

class MyMouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent event)
{
  JTable table = (JTable) event.getSource();
  if (event.getClickCount()==2)
  {
     int row = table.rowAtPoint(event.getPoint());
     System.out.println(Row d-clicked: "+row);
  }
}
}
0
 

Expert Comment

by:cini_gm
ID: 6968294
Pls go thru this simple eg: This will help u..

import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.*;

public class Class1 extends JFrame  {
        JScrollPane jScrollPane1 = new JScrollPane();
         
       JTable editableTable1 = new JTable(3, 3);    
        public Class1() {
               try {          
         
               this.getContentPane().setLayout(null);
               jScrollPane1.setBounds(new Rectangle(25, 19, 330, 180));
               this.getContentPane().add(jScrollPane1, null);
               jScrollPane1.getViewport().add(editableTable1, null);
               addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent we) {
                         System.exit(0);
                    }
               });
               editableTable1.addMouseListener(new Mouse());
          } catch(Exception e) {
               System.out.println("Exception in Class1:"+e);
          }
        }
     class Mouse extends MouseAdapter {
          public void mouseClicked(MouseEvent me) {
               int mouseX = me.getX();
               int mouseY = me.getY();
                     Point row = new Point(mouseX, mouseY);
                     int pos = editableTable1.rowAtPoint(row);
               System.out.println("Position :"+pos);
          }
     }
        public static void main(String[] args) {
                Class1 class1 = new Class1();
                class1.setSize(400, 400);
                class1.setVisible(true);
               
        }
}

   
0
 

Author Comment

by:kianh
ID: 6968386
Hi,
Thank for all of your proposal.
I can learn more from all of you.
And in this case, I suppose that objects' solution is suitable to me.
Another time, thanks all of you.
Good luck Bye
0
 
LVL 92

Expert Comment

by:objects
ID: 6969857
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ArrayLists in ArrayLists/HashMaps? 4 40
java set up 1 47
wordcount challenge 11 76
thymeleaf natural templating vs JSP 2 27
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

762 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

19 Experts available now in Live!

Get 1:1 Help Now