Solved

Another JTable question !

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

696 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