Buongiorno marbio17, facciamo così, adesso posto un pò di codice, dimmi dove metteresti il repaint()..
allora mi sono costruito un Layout a casella, quindi la mia JTable sta in un box orizzontale.
codice:
Box b5= Box.createVerticalBox();
Box b6= Box.createHorizontalBox();
b6.add(new JLabel("Contenuti presenti nel database"));
b5.add(b6);
b5.add(Box.createVerticalStrut(5));
//JTable
b6=Box.createHorizontalBox();
//classe che mi gestisce la tabella
MyTableModel model = new MyTableModel();
table.setFillsViewportHeight(true);//mi rende la parte senza valori dello stesso colore
initColumnSizes(table);//setta la larghezza delle colonne.
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(false);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
jsp= new JScrollPane();
jsp.getViewport().add(table);//metto alla tabella uno scrollpane
b6.add(jsp);
b5.add(b6);//aggiunta la tabella
//Classe che gestisce la tabella JTable
codice:
class MyTableModel extends AbstractTableModel {
private TableColumn column = null;
private Object[][] data;//matrice di oggetti
private String[] columnNames = {"Titolo", // titoli delle colonne
"Autore",
"Tipologia",
"Nome file",
"Mb",
"Supporto",
"Indice",
"Commenti"};
public MyTableModel(){
temp =mt.trova();//metodo che mi raccoglie i dati dopo una query al DB
data=new Object[temp.length][8];
for(int i=0;i<temp.length;i++){
data[i][0]=temp[i].getTitolo();
data[i][1]=temp[i].getAutore();
data[i][2]=temp[i].getTipologia();
data[i][3]=temp[i].getNome_file();
data[i][4]=temp[i].getDimensione();
data[i][5]=temp[i].getSupporto();
data[i][6]=temp[i].getIdsupporto();
data[i][7]=temp[i].getCommento();
}
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* questo metodo rende le celle editabili
*/
public boolean isCellEditable(int row, int col) {
return true;
//if (col < 2) {
// return false;
//} else {
// return true;
//}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
if (DEBUG) {
System.out.println("Setting value at " + row + "," + col
+ " to " + value
+ " (an instance of "
+ value.getClass() + ")");
}
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
System.out.println("New value of data:");
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
e questo è l'action listener del pulsante "cancella" che mi fa sparire i dati dal database ma non la riga selezionata, quindi non fa il refresh.
codice:
if (o instanceof JButton) {
if(o==cancella){
try{
if(table.getSelectedRow()==-1){//se non hai selezionato niente
JOptionPane.showMessageDialog(null,"Non hai selezionato niente", "Attenzione",JOptionPane.ERROR_MESSAGE);
}else{
int temp[] = table.getSelectedRows();//creo l'array delle righe selezionate
int i=0;
while (i<temp.length){
String titolo = (String)table.getValueAt(temp[i],0);
String autore = (String)table.getValueAt(temp[i],1);
String tipologia = (String) table.getValueAt(temp[i],2);
String nome_file = (String) table.getValueAt(temp[i],3);
int dimensione = (Integer)table.getValueAt(temp[i],4);
String supporto = (String) table.getValueAt(temp[i],5);
String idsupporto = (String) table.getValueAt(temp[i],6);
String commento = (String) table.getValueAt(temp[i],7);
VoceDistributore v=new VoceDistributore(titolo,autore,tipologia,nome_file,dimensione,supporto,idsupporto,commento);
mt.cancella(v); //chiamo il metodo per la cancellazione sul db che funziona
i++;
}
new MyTableModel();//qui ho provato a richiamare la classe per ricaricare i dati
}
}
catch(VoceAssente e){
JOptionPane.showMessageDialog(null,"Non esiste l'oggetto selezionato", "Attenzione",JOptionPane.ERROR_MESSAGE);
}
}
ecco io adesso come faccio a fare sto benedetto refresh con questa situazione..sto uscendo pazzo eh eh..
ciao e grazie