Validate entry in a JTable (Java)

0

I have a problem with JTable: I have a table with the columns CODE, DESCRIPTION, QUANTITY, PRICE, CATALOG, and NUMBER_PAGE, and I need the code, quantity and price columns to only accept numbers. I have tried the events and I can not solve.

This is my code:

public Principal() throws UnsupportedLookAndFeelException, ParseException {
        initComponents();

        SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.BusinessBlackSteelSkin");
        //agrega los campos a la tabla
        String [] col = {"Codigo","Descripción","Cantidad","Precio","Catálogo","#Página"};
        modelo = new DefaultTableModel(col,1);

        tabla.setModel(modelo);
        frameIngresaOrden.dispose();
        frameModOrdenes.dispose();
        frameVerOrdenes.dispose();
        this.setExtendedState(MAXIMIZED_BOTH);
        Pane.add(frameIngresaOrden);
        Pane.add(frameModOrdenes);
        Pane.add(frameVerOrdenes);

         TableColumn catalogo = tabla.getColumnModel().getColumn(4);
         TableColumn numeroPag = tabla.getColumnModel().getColumn(5);

         comboBox.addItem("ÉSIKA");
         comboBox.addItem("CYZONE");
         comboBox.addItem("L'BEL");

         llenarComboBox();

         catalogo.setCellEditor(new DefaultCellEditor(comboBox));
         numeroPag.setCellEditor(new DefaultCellEditor(comboBox2));

        int rw=tabla.getSelectionModel().getMinSelectionIndex();
        int cl=tabla.getColumnModel().getSelectionModel().getLeadSelectionIndex();


    }

Actually I do not carry much of the code because I was designing the interface and from there starting to program the data entry. I leave the image as a reference; The idea is to fill in the data cell by cell:

    
asked by oscar garita 30.06.2017 в 18:50
source

1 answer

1

Set the type of field in the table model

tabla.setModel(new javax.swing.table.DefaultTableModel(
        null,
        new String [] {
            //Defines TODOS los nombres de las columnas que tendrá la tabla
            "Codigo", "Cantidad","Nombre","Precio"
        }
    ) {
        Class[] types = new Class [] {
            //Defines el tipo que admitirá la COLUMNA, cada uno con el índice correspondiente
            //Codigo (Integer), Cantidad (Integer), Nombre (String), Precio(Double)
            java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class, java.lang.Double.class
        };

        //Función que asignará el tipo de campo que asignaste previamente
        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
    });
    
answered by 01.07.2017 в 00:55