Ciao a tutti,
sto costruendo una JTable, ed ho il problema che quando scrivo un valore in una cella, la stringa viene attaccata a quella già presente nella cella invece di sostituirla.
Vorrei che come in excell, appena comincio a scrivere in 1 cella, questa diventi vuota.
Allego il codice della mia tabella, non è un capolavoro : ) sto ancora cercando di capire quando convenga "DefaultTableCellRenderer" o "AbstractTableModel" e come funzionano.
Grazie per l'aiuto.
codice:
public void Tabella() {
final JTable TableListino = new JTable(new MyTableModel());
TableListino.setCellSelectionEnabled(true);
TableListino.setAutoResizeMode (JTable.AUTO_RESIZE_ALL_COLUMNS);
TableColumn col = TableListino.getColumnModel().getColumn(0);
col.setCellRenderer(new myRender());
ScrollListino.getViewport().add(TableListino);
System.out.println("fine Tabella()");
}
}
class MyTableModel extends AbstractTableModel {
private String[] colonne = arraiColTabella;
private Object[][] righe = arraiDatiTabella;
boolean rowSelectionAllowed=false;
public int getColumnCount() {
return colonne.length;
}
public int getRowCount() {
return righe.length;
}
@Override
public String getColumnName(int col) {
return colonne[col];
}
public Object getValueAt(int col,int row) {
return righe[col][row];
}
public void setData(String[][] newdata) {
righe = newdata;
fireTableDataChanged();
}
@Override
public Class getColumnClass(int c) {
return getValueAt(c, 0).getClass();
}
public boolean isCellSelected(int row,int column) {
if (row > 2) {
return false;
} else {
return true;
}
}
@Override
public boolean isCellEditable(int col,int row) {
if (row > 2) {
return false;
} else {
return true;
}
}
@Override
public void setValueAt(Object value, int col,int row) {
if (DEBUG) {
}
righe[col][row]= value;
fireTableCellUpdated(col,row);
if (DEBUG) {
printDebugData();
}
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
for (int j=0; j < numCols; j++) {
}
}
}
}
class myRender extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent
(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent
(table, value, isSelected, hasFocus, row, column);
if(row==0) cell.setBackground(Color.red);
else
cell.setBackground(Color.green );
return cell;
}
}