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!