Jtable only accept float type numbers in your cell [closed]

-1

I am using this code to accept only numbers in the jtable cell, but I would like to know how to accept . as well. Can someone 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 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;
        }
    }
};
    
asked by anonymous 30.06.2017 / 16:16

1 answer

1

A solution without much change to your code is to add one more condition after it checks that the character is a digit, which will check if the character is not a floating point either:

for (int i = 0; i < s.length(); i++) {
    Character caractere = s.charAt(i);
    if (!Character.isDigit(caractere)) {
        if(!caractere.equals('.')){
        valido = false;
        break;
        }
    }
}

But since the goal is to only allow floating-point numbers, I think using TableModel is a lot easier. What you need to do is just set in the method getColumnClass that in the second column is type Float . See the example below:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JTableTest extends JFrame {

    public void createAndShowGUI() {

        Object columnNames[] = { "Any type Column", "Only Float Column",};

        DefaultTableModel model = new DefaultTableModel(null, columnNames) {

            @Override
            public Class<?> getColumnClass(int columnIndex) {

                    return columnIndex == 1 ? Float.class : super.getColumnClass(columnIndex); 
            }
        };

        model.addRow(new Object[]{});

        JTable table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);

        this.add(scrollPane, BorderLayout.CENTER);

        pack();
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String args[])  {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new JTableTest().createAndShowGUI();

            }
        });
    }
}

Running:

    
30.06.2017 / 17:18