Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Another JTable question !

Posted on 2002-04-25
5
Medium Priority
?
188 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
[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
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 200 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

704 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