Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Another JTable question !

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

Hire Technology Freelancers with Gigs

Work with 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

916 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