Question

I have a JFrame with a JTable inside, but when code launch the JFrame it don't show Jtable component, the frame shows empty but the data in the Object "datos" its correct.

This frame is launched before a Login screen.

This is the JFrame code:

public class PrincipalWindow {

    private void initialize() {
        Principal principal = new Principal();
        DefaultTableModel modelo = new DefaultTableModel();

        JTable table = new JTable(modelo);
        JFrame f = new JFrame();

        modelo = principal.inicializaModelo();

        f.setBounds(10, 10, 800, 600);
        f.getContentPane().add(new JScrollPane(table));


        f.setVisible(true);

    }
}

Principal.java

public class Principal {
    List<CatalogoVO> listaCatalogo;


    public List<CatalogoVO> getListaCatalogo() {
        return listaCatalogo;
    }


    public void setListaCatalogo(List<CatalogoVO> listaCatalogo) {
        this.listaCatalogo = listaCatalogo;
    }

    public DefaultTableModel inicializaModelo(){
        DefaultTableModel modelo = new DefaultTableModel();
        CatalogoDAO catalogoDAO = new CatalogoDAO();

        modelo.addColumn(Constantes.COL_REGISTRO);
        modelo.addColumn(Constantes.COL_MATERIA);
        modelo.addColumn(Constantes.COL_TITULO);
        modelo.addColumn(Constantes.COL_TEXTO);
        modelo.addColumn(Constantes.COL_FECHA);
        modelo.addColumn(Constantes.COL_CONVOCATORIA);
        modelo.addColumn(Constantes.COL_LUGAR);
        modelo.addColumn(Constantes.COL_IDIOMA);
        modelo.addColumn(Constantes.COL_DESCRIPCION);
        modelo.addColumn(Constantes.COL_INGRESO);
        modelo.addColumn(Constantes.COL_UBICACION);
        modelo.addColumn(Constantes.COL_SOPORTE);
        modelo.addColumn(Constantes.COL_DIMENSIONES);
        modelo.addColumn(Constantes.COL_IMPRESOR);
        modelo.addColumn(Constantes.COL_DEPOSITO);
        modelo.addColumn(Constantes.COL_CONSERVACION);
        modelo.addColumn(Constantes.COL_OBSERVACIONES);

        listaCatalogo = catalogoDAO.getListaCompleta();

        Object [] datos; //Crea un vector
        for(int i=0;i<listaCatalogo.size();i++){
            datos = new Object[19];
            datos[0] = listaCatalogo.get(i).getRegistro();
            datos[1] = listaCatalogo.get(i).getMateria();
            datos[2] = listaCatalogo.get(i).getTipoDocumental();
            datos[3] = listaCatalogo.get(i).getTitulo();
            datos[4] = listaCatalogo.get(i).getTexto();
            datos[5] = listaCatalogo.get(i).getFecha();
            datos[6] = listaCatalogo.get(i).getConvocatoria();
            datos[7] = listaCatalogo.get(i).getLugar();
            datos[8] = listaCatalogo.get(i).getIdioma();
            datos[9] = listaCatalogo.get(i).getDescripcion();
            datos[10] = listaCatalogo.get(i).getAutor();
            datos[11] = listaCatalogo.get(i).getFormaIngreso();
            datos[12] = listaCatalogo.get(i).getUbicacion();
            datos[13] = listaCatalogo.get(i).getSoporte();
            datos[14] = listaCatalogo.get(i).getDimensiones();
            datos[15] = listaCatalogo.get(i).getImpresor();
            datos[16] = listaCatalogo.get(i).getDepositoLegal();
            datos[17] = listaCatalogo.get(i).getConservacion();
            datos[18] = listaCatalogo.get(i).getObservaciones();

            modelo.addRow(datos);

        }

        return modelo;
    }
}
Was it helpful?

Solution

Let's analyze the code, line by line:

DefaultTableModel modelo = new DefaultTableModel();

Here you create an empty table model

JTable table = new JTable(modelo);

Here you create a table using this empty table model

modelo = principal.inicializaModelo();

And here you reassign the modelo variable with a non-empty table model. The table is still using the empty table model.

What you want is:

DefaultTableModel modelo = principal.inicializaModelo();
JTable table = new JTable(modelo);

OTHER TIPS

That's because your JTable still has an empty model. You first need to initialize your model and then create the JTable (or set the model afterwards with table.setModel(model).

Change this line

f.getContentPane().add(new JScrollPane(table));

to this line

f.setContentPane(new JScrollPane(table));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top