dshrenik
asked on
Java Swing: JPanel
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(ListSelection Event arg0), but not sure how to do it.
If possible, please provide some sample code.
Thanks!
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(ListSelection
If possible, please provide some sample code.
Thanks!
you may have several lists - and determine within the method which of the lists was clicked
- that would be analog of the column
- that would be analog of the column
this is about the JTable and List Selection Listener
http://www.exampledepot.com/egs/javax.swing.table/SelEvent.html
http://www.exampledepot.com/egs/javax.swing.table/SelEvent.html
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
}
}
}
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
These are two methods offered in the above link which use MouseListener - seems to be easier
than wit ListSelectionListemerr
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) {
......
}
}
});
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...
...
ASKER
Can you tell me what exactly the above 2 methods are doing?
ASKER
I mean, how are they different from each other?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So where dies the above code go? In the constructor?
Yes, in the way they sjhow it - it goes into the constructor
ASKER
Great. Thanks!
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(t his);
and then in
public void mouseClicled(MouseEvent e){
if(e.getSource().equals(jT able1)){
int row = jTable1.rowAtPoint(e.getPo int());
int col = jTable1.columnAtPoint(e.ge tPoint());
if (row >= 0 && col >= 0) {
......
}
But most folks use anonymouis classes.
I would rather declare on top fo the class implements MouseListenere
then I'd say in constructo
jTbale1.addMouseListener(t
and then in
public void mouseClicled(MouseEvent e){
if(e.getSource().equals(jT
int row = jTable1.rowAtPoint(e.getPo
int col = jTable1.columnAtPoint(e.ge
if (row >= 0 && col >= 0) {
......
}
But most folks use anonymouis classes.
ASKER
jTbale1.addMouseListener(t his);
the above line of code is not working. It says the argument is not applicable.
the above line of code is not working. It says the argument is not applicable.
Did you say at the top "implements MouseListener" ?
ASKER
Oops! Thanks!
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
}
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
}
ListSelectionListener is about list
list.getSelectedIndex() will return the index of selected item in the list