Solved

Java Swing: JPanel

Posted on 2011-09-09
18
434 Views
Last Modified: 2012-08-13
I have a class A that extends JPanel and implements ListSelectionListener.
When the user clicks on a table cells, please let me know how I can determine the row number, and column number of the clicked cell.

I feel I must do this within the method valueChanged(ListSelectionEvent arg0), but not sure how to do it.

If possible, please provide some sample code.

Thanks!
0
Comment
Question by:dshrenik
  • 12
  • 6
18 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36515080

ListSelectionListener is about list

list.getSelectedIndex() will return the index of selected item in the list
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515082
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515083
you may have several lists - and determine within the method which of the lists was clicked
- that would be analog of the column
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 47

Expert Comment

by:for_yan
ID: 36515086
this is about the JTable and List Selection Listener

http://www.exampledepot.com/egs/javax.swing.table/SelEvent.html
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515089

Yes, I was wrong initially - of course you can use for JTable, like in this code from above link:
SelectionListener listener = new SelectionListener(table);
table.getSelectionModel().addListSelectionListener(listener);
table.getColumnModel().getSelectionModel()
    .addListSelectionListener(listener);

public class SelectionListener implements ListSelectionListener {
    JTable table;

    // It is necessary to keep the table since it is not possible
    // to determine the table from the event's source
    SelectionListener(JTable table) {
        this.table = table;
    }
    public void valueChanged(ListSelectionEvent e) {
        // If cell selection is enabled, both row and column change events are fired
        if (e.getSource() == table.getSelectionModel()
              && table.getRowSelectionAllowed()) {
            // Column selection changed
            int first = e.getFirstIndex();
            int last = e.getLastIndex();
        } else if (e.getSource() == table.getColumnModel().getSelectionModel()
               && table.getColumnSelectionAllowed() ){
            // Row selection changed
            int first = e.getFirstIndex();
            int last = e.getLastIndex();
        }

        if (e.getValueIsAdjusting()) {
            // The mouse button has not yet been released
        }
    }
}

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515096


check this - it shows simpler way to detemrnin which cell was clicked:
http://stackoverflow.com/questions/5044222/how-can-i-determine-which-cell-in-a-jtable-was-selected
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515101
These are two methods offered in the above link which use MouseListener - seems to be easier
than wit ListSelectionListemerr

jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int row = jTable1.rowAtPoint(evt.getPoint());
        int col = jTable1.columnAtPoint(evt.getPoint());
        if (row >= 0 && col >= 0) {
            ......

        }
    }
});

Open in new window


jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
       @Override
       public void mouseClicked(java.awt.event.MouseEvent evt) {
           ...
           int row = jTable1.getSelectedRow();
           int col = jTable1.getSelectedColumn());
           if (evt.getClickCount() > 1) { // double-click etc...
              ...

Open in new window



0
 

Author Comment

by:dshrenik
ID: 36515103
Can you tell me what exactly the above 2 methods are doing?
0
 

Author Comment

by:dshrenik
ID: 36515105
I mean, how are they different from each other?
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36515107

I'd use this one - it is the simplest - it determines
the point of clicking from the MouseEvent
and then using Jtable methods rowAtPoint(Point)
and columnAtPoint(Point)
very starightforwardly detemine the colmun and row number
of the point which was clicked.

jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int row = jTable1.rowAtPoint(evt.getPoint());
        int col = jTable1.columnAtPoint(evt.getPoint());
        if (row >= 0 && col >= 0) {
            ......

        }
    }
});

Open in new window

0
 

Author Comment

by:dshrenik
ID: 36515111
So where dies the above code go? In the constructor?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515114
Yes, in the way they sjhow it - it goes into the constructor
0
 

Author Comment

by:dshrenik
ID: 36515115
Great. Thanks!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515118
I don't like this anonymous classes
I would rather declare on top fo the class implements MouseListenere

then I'd say in constructo
jTbale1.addMouseListener(this);



and then in

public void mouseClicled(MouseEvent e){

if(e.getSource().equals(jTable1)){
 int row = jTable1.rowAtPoint(e.getPoint());
        int col = jTable1.columnAtPoint(e.getPoint());
        if (row >= 0 && col >= 0) {
            ......

        }

But most folks use anonymouis classes.


 
0
 

Author Comment

by:dshrenik
ID: 36515125
jTbale1.addMouseListener(this);

the above line of code is not working. It says the argument is not applicable.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515132
Did you say at the top "implements MouseListener" ?
0
 

Author Comment

by:dshrenik
ID: 36515136
Oops! Thanks!
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36515140
And you need to define all methods of MouseListener

just paste these:
 public void mousePressed(MouseEvent me){}

 public void mouseEntered(MouseEvent me){}

public void mouseExited(MouseEvent me){}

public void mouseReleased(MouseEvent me){}

and in this ione
put real code:

public void mouseClicked() {

///stuuff

}
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 50
advertisement module in core php 4 159
type mismatch (Object[] to double[] 4 23
runtime exception 2 27
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

813 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

8 Experts available now in Live!

Get 1:1 Help Now