JTable does not appear in the frame

1

I have the following code:

public class FacturacionGenericaApp extends JFrame {

    // Componentes Graficos
    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FacturacionGenericaApp window = new FacturacionGenericaApp();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public FacturacionGenericaApp() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1129, 519);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        this.cargarDatosDB();

        //create table with data
        JTable table = this.cargarTabla();
        frame.getContentPane().add(table);

    }

    private JTable cargarTabla() {   
        try {
            // The Connection is obtained
            Statement stmt = conexionDB.createStatement();
            ResultSet rs = stmt .executeQuery("select * from clientes");

            // It creates and displays the table
            return new JTable(construirModeloDeTabla(rs));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    private TableModel construirModeloDeTabla(ResultSet rs) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }

        return new DefaultTableModel(data, columnNames);
    }

}

The problem is that when the application starts, the table does not appear:

But if within cargarTabla() I put the following line:

JOptionPane.showMessageDialog(null, new JScrollPane(table));

It prints on the screen perfectly! So the information is loading perfectly, just do not insert it in the frame.

What am I missing?

Thank you very much!

    
asked by Genarito 22.05.2017 в 18:23
source

1 answer

1

The option of not to use Layout for a container ie .setLayout(null); is that you have to specify in which position and the size of the element to add using the setBounds method (x, y, width, height)

 frame.getContentPane().setLayout(null);
 JTable table = this.cargarTabla();
 frame.add(table);
 table.setBounds (10,10,200,200);
  

Although the recommendation is to use Layouts to manage the   components in your container

    
answered by 22.05.2017 / 18:44
source