Posto il codice pagina principale renderer ed editor il problema è che una volta usato l'evento del mouse mi stampa tramite l'editor gli elementi della riga della tabella selezionata più volte in modo incrementale cioè al primo click una volta al secondo due e così via cosa ho sbagliato?
________________________________________________________________________
//codice pag principale
package it.view;
import it.DBConnection.DbConnection;
import it.view.logIn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class dipendente extends JFrame{
private DefaultTableModel defTableModel;
private JTable table;
int k;
public dipendente(){
super("Dipendente"); //titolo pagina
setSize(1800,1000); //grandezza pagina
setLocation(50,15);
setLayout(null);
//immagini bottoni
ImageIcon imgc=new ImageIcon("car.gif");
ImageIcon logO=new ImageIcon("logout.gif");
ImageIcon cerc=new ImageIcon("cerca.gif");
//creo tipo di font
Font f=new Font("times new roman",Font.PLAIN,25);
Font t=new Font("times new roman",Font.PLAIN,20);
//creo contenitore
Container c =getContentPane();
//creo i label
JLabel Ben=new JLabel("Benvenuto "+logIn.U.getText()+" !");
JLabel Cat=new JLabel("Categoria");
final JLabel a=new JLabel("Vuoto");
//creo i button
JButton Esci=new JButton(logO);
JButton car=new JButton(imgc);
JButton cerca=new JButton(cerc);
//creo menu tendina
final JComboBox<String> Cate = new JComboBox<String>();
//query al dataBase per le categorie
String sql="Select DISTINCT Nome from Categoria;";
Vector<String[]>result=DbConnection.getInstance().eseguiQuery(sql);
Iterator<String[]>i=result.iterator();
//creo modello tabella e rendo righe non selezionabili
defTableModel = new DefaultTableModel(){
public boolean isCellEditable(int row, int column) {
return ((column==6)||(column==7));
}
};
table = new JTable(defTableModel);
table.setRowHeight(26);
//scorre nell'array
while(i.hasNext()){
String[] riga = i.next();
Cate.addItem(riga[0]);
}
cerca.addActionListener(new ActionListener(){
DefaultTableModel model = (DefaultTableModel) table.getModel();
public void actionPerformed(ActionEvent e){
String s=Cate.getSelectedItem().toString();
String sql="Select IdCategoria from Categoria where Nome='"+s+"';";
Vector<String[]>result=DbConnection.getInstance().eseguiQuery(sql);
Iterator<String[]>i=result.iterator();
//preleva gli articoli di una stessa categoria
while(i.hasNext()){
Object[]riga=i.next();
String sql1="Select * from Articolo where CodCat='"+riga[0]+"'And NmaxOrd>=5;";
Vector<String[]>result1=DbConnection.getInstance().eseguiQuery(sql1);
Iterator<String[]>j=result1.iterator();
//avvalora tabella
while(j.hasNext()){
Object[]riga1=j.next();
Object[][] data = result1.toArray(new Object[0][]);
defTableModel.setDataVector(data, new Object[] {"IdArticolo","Nome","Descrizione","Costo","Disponibilita'","NMaxOrd","Quantità"," Selezione "});
}
}
//avvaloro JCombobox
TableColumn Quantità = table.getColumnModel().getColumn(6);
JComboBox<Integer> nOrd = new JComboBox<Integer>();
int v=0;
while(v<=5){
nOrd.addItem(new Integer(v));
v++;
}
Quantità.setCellEditor(new DefaultCellEditor(nOrd));
//cella aggiunta al carrello
EditorBottone editBotton=new EditorBottone();
CellRenderer renderer= new CellRenderer();
table.getColumnModel().getColumn(7).setCellRenderer(renderer);
table.getColumnModel().getColumn(7).setCellEditor(editBotton);
}//chiude l'action performed
}); //chiude actionlistener
JScrollPane pane=new JScrollPane(table);
//imposto font
Ben.setFont(f);
Cat.setFont(f);
Esci.setFont(f);
car.setFont(f);
Cate.setFont(f);
pane.setFont(f);
table.setFont(t);
Ben.setBounds(1350,10,200,50);
Esci.setBounds(1575,10,50,50);
car.setBounds(1650,10,50,50);
Cate.setBounds(800,100,Cate.getPreferredSize().width,50);
cerca.setBounds(1000, 100, 100, 50);
pane.setBounds(200,300,1500,pane.getPreferredSize().height);
c.add(Ben);
c.add(Esci);
c.add(car);
c.add(Cate);
c.add(cerca);
c.add(pane);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
_______________________________________________________________________________________________
//Codice cell Renderer
package it.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class CellRenderer implements TableCellRenderer{
public Component getTableCellRendererComponent(final JTable table,Object value, boolean isSelected, boolean hasFocus, int row,int column){
Font f=new Font("times new roman",Font.PLAIN,18);
JButton cella=new JButton("Aggiungi al carrello"); // Questa rappresenta la singola cella
cella.setFont(f);
return cella;
}
}
______________________________________________________________________________________________________
//codice editor bottone in cui ho inserito l'evento
package it.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
public class EditorBottone extends AbstractCellEditor implements TableCellEditor
{
@Override
public Component getTableCellEditorComponent(final JTable table, Object arg1,
boolean arg2, int arg3, int arg4) {
System.out.println("ooooooook");
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
String col1=null, col2=null, col3=null,col4=null,col5=null,col6=null;
int col7=0;
col1 = (String) table.getValueAt(table.getSelectedRow(), 0);
col2 = (String) table.getValueAt(table.getSelectedRow(), 1);
col3 = (String) table.getValueAt(table.getSelectedRow(), 2);
col4 = (String) table.getValueAt(table.getSelectedRow(), 3);
col5 = (String) table.getValueAt(table.getSelectedRow(), 4);
col6 = (String) table.getValueAt(table.getSelectedRow(), 5);
col7 = (int) table.getValueAt(table.getSelectedRow(), 6);
if((col1.length() != 0) && (col2.length() != 0)) System.out.println("Contenuto riga selezionata: "+col1+" "+col2+" "+col3+" "+col4+" "+col5+" "+col6+" "+col7);
}
});
return null;
}
@Override
public Object getCellEditorValue() {
// TODO Auto-generated method stub
return null;
}
}