ola pessoal …
criei 2 classes para formatar os campos de uma tabela (pra valores monetarios) usando celleditor e cellrenderer …
ai vai minha tabela :
modelo = new DefaultTableModel( dado , colu );
jTable1 = new JTable( modelo ); // linha e coluna
jTable1.setVisible(true);
jTable1.getTableHeader().setReorderingAllowed(false);
jTable1.getTableHeader().setResizingAllowed(false);
jTable1.getTableHeader().setBackground( Color.lightGray ) ;
jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable1.setGridColor( Color.black );
jTable1.setShowHorizontalLines(true) ;
jTable1.setShowVerticalLines(true) ;
jTable1.setEnabled(true);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.getColumnModel().getColumn(0).setPreferredWidth( 100 );
jTable1.getColumnModel().getColumn(1).setCellEditor(new MoedaCellEditor());
jTable1.getColumnModel().getColumn(1).setCellRenderer(new MoedaCellRender());
modelo.setRowCount(10);
public class MoedaCellRender extends JLabel implements TableCellRenderer {
/**
* classe para mostrar a celula com formato de moeda
*/
private static final long serialVersionUID = 1L;
private JLabel cell = null;
private NumberFormat formatter = NumberFormat.getCurrencyInstance( new Locale("pt","BR") ); // Locale.getDefault()
private JLabel getCell() {
if (cell == null)
cell = new JLabel();
return cell;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value != null) {
getCell().setText( formatter.format(value) );
getCell().setHorizontalAlignment(SwingConstants.RIGHT);
} else getCell().setText("");
return getCell();
}
}
public class MoedaCellEditor extends AbstractCellEditor implements TableCellEditor {
/**
* classe para editar a celula com formato de moeda
*/
private static final long serialVersionUID = 1L;
private JFormattedTextField moeda = null;
private NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("pt","BR"));
private JFormattedTextField getCell() {
if (moeda == null)
moeda = new JFormattedTextField(formatter);
return moeda;
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
getCell().setValue(value);
return getCell();
}
public Object getCellEditorValue() {
return getCell().getValue();
}
}
quando executo o programa, ele aparece normalmente com a formatacao, mas quando digito alguma coisa no campo, um valor qualquer, na saida do focus ele fica em branco, como se eu nao tivesse digitado nada, o que pode se …
a tabela ta com o comando pra ser editavel