JTable with TableCellEditor ???
Hello
I have a JTable and I created a table cell editor for that can use a
JTextField customized, this is working. Now I need that when I press
enter in cell selected the edit is open in JTable and not change the
row before change.
The situation looks like below
Enter on Selected Cell --> open the edit (enter value = US$ 1.00) -->
press enter again, close the edit and request focus in next cell to
make the process again.
/* AbstractTableModel - Brazilian Portuguese US$ = R$ */
public class TipoPagtoTableModel extends AbstractTableModel{
private List<TipoPagto> listaTipoPagto;
private String[] colunas = {"Id","Tipo Pagto.","Valor(R$)"};
public TipoPagtoTableModel(){
this.listaTipoPagto = new ArrayList<TipoPagto>();
}
public TipoPagtoTableModel(List<TipoPagto> lista){
this();
this.listaTipoPagto.addAll(lista);
}
@Override
public int getRowCount() {
return this.listaTipoPagto.size();
}
@Override
public int getColumnCount() {
return this.colunas.length;
}
public String getColumnName(int column){
//nome da coluna
if(colunas[column] == "Id"){
return "Id";
}else if(colunas[column] == "Tipo Pagto."){
return "Tipo Pagto.";
}else if(colunas[column] == "Valor(R$)"){
return "Valor(R$)";
}
return new String();
}
public Class getColumnClass(int column){
if(colunas[column] == "Id"){
return Long.class;
}else if(colunas[column] == "Tipo Pagto."){
return String.class;
}else if(colunas[column] == "Valor(R$)"){
return BigDecimal.class;
}
return String.class;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TipoPagto tp = this.listaTipoPagto.get(rowIndex);
switch(columnIndex){
case 0: return new
FormataCodigos().retornaCodigoFormatado(tp.getIdPagto());
case 1: return tp.getTipo();
case 2: return tp.getValor();
default: return new String();
}
}
public void setValueAt(Object aValue, int rowIndex, int
columnIndex) {
listaTipoPagto.get(rowIndex).setValor(new
BigDecimal(aValue.toString()));
fireTableRowsUpdated(rowIndex, columnIndex);
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(columnIndex == 2){
return true;
}else{
return false;
}
}
public Boolean deleteAll(){
this.listaTipoPagto.clear();
fireTableDataChanged();
return true;
}
public boolean removeRow(int line){
this.listaTipoPagto.remove(line);
fireTableDataChanged();
return true;
}
}
/* TableCellEditor */
public class TipoPagtoTableCellEditor extends AbstractCellEditor
implements TableCellEditor {
private JTextField txtField;
public TipoPagtoTableCellEditor(){
txtField = new JMoneyField(15);
txtField.setFont(new Font("Tahoma", Font.BOLD, 12));
}
@Override
public Object getCellEditorValue() {
return new BigDecimal(txtField.getText().replaceAll("\\.",
"").replace(",", "."));
}
@Override
public Component getTableCellEditorComponent(JTable table, Object
value, boolean isSelected, int row, int column) {
txtField.setText((String)value);
return txtField;
}
}
/* JFrame */
List<TipoPagto> lista = new TipoPagtoDAO().getFormaPagto();
TipoPagtoTableModel model = new TipoPagtoTableModel(lista);
tabelaTipoPagto.setModel(model);
TableColumn col = tabelaTipoPagto.getColumnModel().getColumn(2);
col.setCellEditor(new TipoPagtoTableCellEditor());
How do this ? Any idea ?
thanks.