Solved

Another JTable question !

Posted on 2002-04-25
5
186 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

626 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