SelectionListener Problem
Hi!
I have a JTable on which added a SelectionListener. I want to do something evrytime some cell is selected. The problem is that eveytime I click on a cell the event is produced two times.
I have the following code. Hope someone can help. Thanks in advance.
package editorhorarios;
import java.sql.SQLException;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.*;
import com.nqadmin.swingSet.datasources.SSJdbcRowSetImpl;
import com.nqadmin.swingSet.datasources.SSConnection;
import com.nqadmin.swingSet.*;
import java.util.*;
publicclass SelectionListenerHorarioimplements ListSelectionListener{
SSDataGrid sSDataGrid_hor;
SSJdbcRowSetImpl rowset=new SSJdbcRowSetImpl();
SSConnection connection;
Vector modulos=new Vector();
DefaultTableModel dtm=new DefaultTableModel();
// It is necessary to keep the table since it is not possible
// to determine the table from the event's source
SelectionListenerHorario(SSDataGrid datagrid2){
this.sSDataGrid_hor=datagrid2;
//this.rowset.setSSConnection(con);
}
publicvoid valueChanged(ListSelectionEvent e){
// If cell selection is enabled, both row and column change events are fired
if (e.getSource() == sSDataGrid_hor.getSelectionModel()
&& sSDataGrid_hor.getRowSelectionAllowed() && sSDataGrid_hor.getColumnSelectionAllowed() && !e.getValueIsAdjusting() ){
int index=sSDataGrid_hor.getSelectedRow();
if(index!=-1){
Object valor2=null;
try{
valor2=this.sSDataGrid_hor.getValueAt(index,this.sSDataGrid_hor.getSelectedColumn());
System.out.println("valor2="+valor2.toString());
}catch(Exception ex){
System.out.println("nao deu2") ;
}
try{
if(!valor2.equals(null)){
Object valor=this.sSDataGrid_hor.getValueAt(index-1,this.sSDataGrid_hor.getSelectedColumn());
if(!valor.equals(null)){
if(valor.toString().equals(valor2.toString())){
System.out.println("igual");
this.sSDataGrid_hor.getSelectionModel().setSelectionInterval(index-1,index);
}
}
}else{
System.out.println("nada");
this.sSDataGrid_hor.clearSelection();
}
}catch(Exception ex){
System.out.println("nao deu2") ;
}
}
}else{
dtm.setNumRows(0);
}
//} else if (e.getSource() == sSDataGrid4.getColumnModel().getSelectionModel()
//&& sSDataGrid4.getColumnSelectionAllowed() ){
//// Row selection changed
//int first = e.getFirstIndex();
//int last = e.getLastIndex();
//System.out.println("seleccionou2");
//}
//if (e.getValueIsAdjusting()) {
//// The mouse button has not yet been released
////System.out.println("seleccionou3");
//}
}
}

