Ciao a tutti!
Girando per Internet ho trovato un pezzo di codice per poter visualizzare una cella di una JTable su più righe e l'ho adattato al mio programma. Il codice è il seguente:
codice:
package MyPackage;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;
public class MyTableCellRenderer extends JTextArea implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public MyTableCellRenderer(){
setLineWrap(true);
setWrapStyleWord(true);
setOpaque(true);
}
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int col) {
if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
}
else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setFont(table.getFont());
if (hasFocus){
if (table.isCellEditable(row,col)) {
setForeground( UIManager.getColor("Table.focusCellForeground") );
setBackground( UIManager.getColor("Table.focusCellBackground") );
}
}
else {
setBorder(new EmptyBorder(1, 2, 1, 2));
}
int rowHeight = (int) getMinimumSize().height;
if (table.getRowHeight() != rowHeight)
table.setRowHeight(row,rowHeight);
setText((value == null) ? "" : value.toString());
return this;
}
}
Il problema è che quando vado a modificare la riga di una tabella, non mi aumenta la dimensione della riga stessa ma di quella sotto!!!
Cioè se ho una tabella con due righe e modifico la riga 0 per vedere se il testo si espande su due righe, mi modifica l'altezza della riga sotto di essa (e viceversa, cioè se modifico la riga 1 mi modifica la riga 0).
Non riesco a capire perché fa così.