-1
I’m using this code to accept only numbers in the jtable cell, but I’d like to know how to accept .
too. Someone can help me?
I’m using Java Language on the netbeans platform.
TableColumn col = tabela.getColumnModel().getColumn(1);
col.setCellEditor(new MyTableCellEditor());
class MyTableCellEditor extends AbstractCellEditor
implements TableCellEditor{
JComponent component = new JTextField();
public boolean stopCellEditing(){
String s = (String)getCellEditorValue();
boolean valido = true;
for(int i = 0; i < s.length(); i++){
Character caractere = s.charAt(i);
if(!Character.isDigit(caractere)){
valido = false;
break;
}
}
if(!valido){
JOptionPane.showMessageDialog(null,
"Valor inválido");
return false;
}
return super.stopCellEditing();
}
public Component getTableCellEditorComponent(
JTable table, Object value,
boolean isSelected, int rowIndex, int vColIndex){
if(isSelected){
//
}
((JTextField)component).setText((String)value);
return component;
}
public Object getCellEditorValue() {
return ((JTextField)component).getText();
}
}
follows the jtable
DefaultTableModel modelo = new DefaultTableModel(null, new String[]{
"ID", // 0
"Ordem", // 1
"Linha", // 2
"Linha_Tipo", // 3
"Setor", // 4
"Perfil", // 5
"Bpcs", // 6
"Desc_Perfil", // 7
"Projeto", // 8
"OEM", // 9
"Nº_Desenho", // 10
"Nº_Plano", // 11
"Operação", // 12
"Equipamento", // 13
"Desc_Teste", // 14
"Complemento", // 15
"Cod_Teste", // 16
"Espec_Min", // 17
"Espec_Max", // 18
"Espec_Unid", // 19
"Espec_Texto", // 20
"Referência", // 21
"Frequência", // 22
"Freq_Unid", // 23
"Produto", // 24
"Origem", // 25
"Tipo", // 26
"Especificação", // 27
"Freq_Texto", // 28
"Laboratorio", // 29
"Resultado_Numerico", // 30
"Resultado_Texto", // 31
"Observação", // 32
"Aprovado"}) { // 33
@Override
public boolean isCellEditable(int linha, int coluna) {
switch (coluna) {
case 30:
case 31:
String tipo = (String) getValueAt(linha, 26);
if (tipo != null) {
switch (tipo) {
case "Min e Max":
case "No Min":
case "No Max":
return coluna == 30;
case "Texto":
return coluna == 31;
default:
return false;
}
}
return false;
case 32:
case 33:
return true;
default:
return false;
}
}
};
Using Tablemodel, just set the column to Double that doesn’t even need to make a celleditor.
– user28595
would have an example?
– Rafael Chaves
Yes, I’m testing a shape here.
– user28595