scusatemi tanto del ritardo ma sn stata presa da molte cose e ... per risponderti dovevo rivedere bene il codice... sai a volte dopo tanto tempo ti dimentichi quello che hai scritto!!!
ecco qui, il dtm viene creato proprio qui all'inizio di questo metodo che identifica la tabella le cui righe vanno poi colorate....
codice:
private JTable getTblMod(final table t,DefaultTableModel dtm,final JTable table) throws SQLException {
// Nomi delle colonne
String str = "SELECT * FROM " + t.getNameTable();
final ResultSet rs=dataBaseConnection.queryDb(str);
rs.last();
//invece che +2 è +1
dtm = new DefaultTableModel (rs.getRow(),t.getSize()+1);
final DefaultTableModel dtm1=new DefaultTableModel (rs.getRow(),t.getSize()+1);
// invece che +2 è +1
Object[] NomeColumn = new Object[t.getSize()+1];
for (int i=0;i<t.getSize();i++)
NomeColumn[i]= (String) t.getAttibutes(i).getName();
//NomeColumn[t.getSize()]=("CANCELLA");
//era getSize+1
NomeColumn[t.getSize()]=("MODIFICA");
int r=0;
rs.beforeFirst();
while (rs.next()) {
for(int c=0; c<t.getSize();c++){
System.out.println(rs.getString(c+1));
dtm.setValueAt(rs.getString(c+1),r,c);
dtm1.setValueAt(rs.getString(c+1),r,c);
}
JButton buttonMod = new JButton(new ImageIcon("swing/Img/matita giusta.jpg"));
dtm1.setValueAt(buttonMod,r,t.getSize());
//era getsize+1
dtm.setValueAt(buttonMod,r,t.getSize());
buttonMod.setBackground(new java.awt.Color(207,203,238));
//Aggiungo dei listener ai bottoni, al loro interno andra' il codice per la rimozione
buttonMod.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
System.out.println("riga selezionata: "+table.getSelectedRow());
int i=0;
int riga=table.getSelectedRow();
ArrayList <String> Kiave=new ArrayList <String>();
Kiave=t.getNameKey();
String query=new String("UPDATE "+dataBaseConnection.getDBSchema().getName()+"."+t.getNameTable()+" SET ");
//query=query+(t.getNameTable()+"."+Kiave.get(0)+" USING utf8)='"+table.getValueAt(riga,t.getKeyColumn().get(0))+"'");
query=query+(t.getAttibutes(0).getName()+" = '"+table.getValueAt(riga,0)+"'");
for(i=1;i<t.getSize();i++){
query=query+(", "+t.getAttibutes(i).getName()+" = '"+table.getValueAt(riga,i).toString()+"'");
}
if(Kiave.size()==1)
query=query+(" WHERE "+t.getNameTable()+"."+Kiave.get(0)+" ='"+dtm1.getValueAt(riga,t.getKeyColumn().get(0))+"'");
else{
query=query+(" WHERE CONVERT ("+t.getNameTable()+"."+Kiave.get(0)+" USING utf8 ) ='"+dtm1.getValueAt(riga,t.getKeyColumn().get(0))+"'");
for(i=1;i<Kiave.size();i++){
query=query+(" AND CONVERT ("+t.getNameTable()+"."+Kiave.get(i)+" USING utf8 ) ='"+dtm1.getValueAt(riga,t.getKeyColumn().get(i))+"'");
}
}
query=query+(" LIMIT 1");
System.out.println("queryyyyyyyyyyyyyyyyyyyyyyy "+query);
try {
@SuppressWarnings("unused") int rsMod=dataBaseConnection.updateDb(query);
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"LOOK OUT!!!: "+e1.getMessage(),"ATTENZIONE",2);
}
}
} );
r++;
}
dtm.setColumnIdentifiers(NomeColumn);
table.setModel(dtm);
// Alla terza colonna,identificata per nome, applico l'editor e il render personalizzati.
table.getColumn("MODIFICA").setCellRenderer(new PanelRender());
table.getColumn("MODIFICA").setCellEditor(new PanelEditor());
table.setRowHeight(25); //Metto un altezza che mi consenta di vedere tutto il bottone
table.setEnabled(true);
table.setRowSelectionAllowed(true);
table.getColumnSelectionAllowed();
table.setAutoscrolls(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setVisible(true);
return table;
}
la classe per intero è come segue....
codice:
public class Tool extends JTabbedPane implements ChangeListener{
private static final long serialVersionUID = 1L;
CallReadFile crf=new CallReadFile();
/*FRAME CONTENITORE*/
JFrame f=new JFrame("DATA CLEANING");
DefaultTableModel dtm;
int n;
/*PANNELLO MENU*/
private JPanel MenuSup=new JPanel();;
private JLabel LblEsci = null;
private JLabel LblDisc = null;
private JLabel LblRefresh=null;
/*PANNELLO MODIFICA*/
private JSplitPane SplitMod;
private JPanel PnlLog;
private JPanel PnlMod;
private JScrollPane ScrollPaneMuseoMod;
public JTable table;
private JScrollPane jScrollPane;
private JEditorPane jEditorPane;
/*ALTRE VARIABILI */
public int typeRich;
int dim=dataBaseConnection.getDBSchema().getSize();
ReadNumMetriche rnm=new ReadNumMetriche();
//****MENU CHIAMA TUTTO****//
public Tool() {
super();
try {
this.initialize();
f.setSize(1000,630);
f.add(this);
f.add(getMenuSup(),null);
f.setVisible(true);
f.setResizable(false);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.addChangeListener(this);
this.setSelectedIndex(1);
this.setSelectedIndex(0);
}
//****MENU INITIALIZE****//
private void initialize() throws SQLException, IOException {
this.setVisible(true);
this.setBounds(0,30,1005,572);
this.setEnabled(true);
//****CREAZIONE PANNELLI****//
for(int i=0;i<dim;i++){
this.addTab(dataBaseConnection.getDBSchema().getTable(i).getNameTable().toUpperCase(), null,new JTabbedPane() , null);
}
}
//****CREAZIONE PANNELLI INTERNI AL PANNELLO PRINCIPALE****//
private JTabbedPane getTbdPane(int i, JTabbedPane TbdPane) throws SQLException {
TbdPane.removeAll();
TbdPane.setName(dataBaseConnection.getDBSchema().getTable(i).getNameTable().toUpperCase());
TbdPane.setTabPlacement(JTabbedPane.LEFT);
table = new JTable(dtm);
SplitMod = new JSplitPane();
//QUI HO PROVATO A COLORARE LE RIGHE MA NULLA DA FARE, SE LE METTO MI SI BLOCCA TUTTA LA SCHERMATA...
/*Vector<Integer> righeRosse = new Vector<Integer>();
righeRosse.add(0);
MioCellRenderer mcr = new MioCellRenderer(righeRosse);
table.getColumnModel().getColumn(0).setCellRenderer(mcr);*/
TbdPane.addTab("MODIFICA", null, getSplitMod(dataBaseConnection.getDBSchema().getTable(i)));
return TbdPane;
}
// SPLITPANE MODIFICA
private JSplitPane getSplitMod(table t) throws SQLException {
ScrollPaneMuseoMod=new JScrollPane();
PnlLog=new JPanel();
SplitMod.setDividerSize(3);
SplitMod.setDividerLocation(250);
SplitMod.setLayout(null);
SplitMod.setOrientation(JSplitPane.VERTICAL_SPLIT);
SplitMod.setTopComponent(getScrollPaneMod(t,dtm,table));
SplitMod.setBottomComponent(getPnlLog());
SplitMod.setVisible(true);
return SplitMod;
}
// SCROLL DELLA TABELLA DEL MUSEO
private JScrollPane getScrollPaneMod(table t,DefaultTableModel dtm,JTable table) throws SQLException {
ScrollPaneMuseoMod.setBounds(new java.awt.Rectangle(0,0,842,533));
table=new JTable();
ScrollPaneMuseoMod.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(238,238,238),5));
ScrollPaneMuseoMod.setViewportView(getTblMod(t,dtm,table));
return ScrollPaneMuseoMod;
}
// TABELLA MUSEO
private JTable getTblMod(final table t,DefaultTableModel dtm,final JTable table) throws SQLException {
// Nomi delle colonne
String str = "SELECT * FROM " + t.getNameTable();
final ResultSet rs=dataBaseConnection.queryDb(str);
rs.last();
//invece che +2 è +1
dtm = new DefaultTableModel (rs.getRow(),t.getSize()+1);
final DefaultTableModel dtm1=new DefaultTableModel (rs.getRow(),t.getSize()+1);
// invece che +2 è +1
Object[] NomeColumn = new Object[t.getSize()+1];
for (int i=0;i<t.getSize();i++)
NomeColumn[i]= (String) t.getAttibutes(i).getName();
//NomeColumn[t.getSize()]=("CANCELLA");
//era getSize+1
NomeColumn[t.getSize()]=("MODIFICA");
int r=0;
rs.beforeFirst();
while (rs.next()) {
for(int c=0; c<t.getSize();c++){
System.out.println(rs.getString(c+1));
dtm.setValueAt(rs.getString(c+1),r,c);
dtm1.setValueAt(rs.getString(c+1),r,c);
}
JButton buttonMod = new JButton(new ImageIcon("swing/Img/matita giusta.jpg"));
dtm1.setValueAt(buttonMod,r,t.getSize());
//era getsize+1
dtm.setValueAt(buttonMod,r,t.getSize());
buttonMod.setBackground(new java.awt.Color(207,203,238));
//Aggiungo dei listener ai bottoni, al loro interno andra' il codice per la rimozione
buttonMod.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
System.out.println("riga selezionata: "+table.getSelectedRow());
int i=0;
int riga=table.getSelectedRow();
ArrayList <String> Kiave=new ArrayList <String>();
Kiave=t.getNameKey();
String query=new String("UPDATE "+dataBaseConnection.getDBSchema().getName()+"."+t.getNameTable()+" SET ");
//query=query+(t.getNameTable()+"."+Kiave.get(0)+" USING utf8)='"+table.getValueAt(riga,t.getKeyColumn().get(0))+"'");
query=query+(t.getAttibutes(0).getName()+" = '"+table.getValueAt(riga,0)+"'");
for(i=1;i<t.getSize();i++){
query=query+(", "+t.getAttibutes(i).getName()+" = '"+table.getValueAt(riga,i).toString()+"'");
}
if(Kiave.size()==1)
query=query+(" WHERE "+t.getNameTable()+"."+Kiave.get(0)+" ='"+dtm1.getValueAt(riga,t.getKeyColumn().get(0))+"'");
else{
query=query+(" WHERE CONVERT ("+t.getNameTable()+"."+Kiave.get(0)+" USING utf8 ) ='"+dtm1.getValueAt(riga,t.getKeyColumn().get(0))+"'");
for(i=1;i<Kiave.size();i++){
query=query+(" AND CONVERT ("+t.getNameTable()+"."+Kiave.get(i)+" USING utf8 ) ='"+dtm1.getValueAt(riga,t.getKeyColumn().get(i))+"'");
}
}
query=query+(" LIMIT 1");
System.out.println("queryyyyyyyyyyyyyyyyyyyyyyy "+query);
try {
@SuppressWarnings("unused") int rsMod=dataBaseConnection.updateDb(query);
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"LOOK OUT!!!: "+e1.getMessage(),"ATTENZIONE",2);
}
}
} );
r++;
}
dtm.setColumnIdentifiers(NomeColumn);
table.setModel(dtm);
// Alla terza colonna,identificata per nome, applico l'editor e il render personalizzati.
table.getColumn("MODIFICA").setCellRenderer(new PanelRender());
table.getColumn("MODIFICA").setCellEditor(new PanelEditor());
table.setRowHeight(25); //Metto un altezza che mi consenta di vedere tutto il bottone
table.setEnabled(true);
table.setRowSelectionAllowed(true);
table.getColumnSelectionAllowed();
table.setAutoscrolls(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setVisible(true);
return table;
}