I would like to further extend the JTable's columns because the information presented is very close and does not appear complete, you have to adjust it manually to be able to visualize all the content.
If I were using model.addColumn("Nombre");
, I could use table.getColumnModel().getColumn(0).setPreferredWidth(30);
but, I do not use this.
Code of my JTable:
// Estableciendo el título de las columnas del JTable
String[] columnas = {"#", "Tipo", "Franja", "Origen", "Destino", "Duracion", "Precio"};
// Creando un DefaultTableModel
DefaultTableModel model = new DefaultTableModel(null, columnas);
// Creando el JTable y asignandole el DefaultTableModel
JTable table = new JTable(model);
// Agregar filas al JTable leyendo la base de datos
String showTableSQL = "SELECT * FROM llamadas";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Object[][] filas = null;
try {
conexion = new ConexionBD();
conn = conexion.getConnection();
st = conn.createStatement();
rs = st.executeQuery(showTableSQL);
int numColumnas = rs.getMetaData().getColumnCount(); // Saber la cantidad de columnas que existen
int numFilas = 0;
while (rs.next()) {
numFilas++;
}
filas = new Object[numFilas][numColumnas];
rs.beforeFirst();
int contadorTmp = 0;
while (rs.next()) {
filas[contadorTmp][0] = Integer.toString(rs.getInt(1)); // ID
filas[contadorTmp][1] = rs.getString(2);
filas[contadorTmp][2] = rs.getString(3);
filas[contadorTmp][3] = String.valueOf(rs.getString(4)).replaceFirst("(\d{3})(\d{3})(\d+)", "($1) $2-$3");
filas[contadorTmp][4] = String.valueOf(rs.getString(5)).replaceFirst("(\d{3})(\d{3})(\d+)", "($1) $2-$3");
filas[contadorTmp][5] = Integer.toString(rs.getInt(6));
filas[contadorTmp][6] = Double.toString(rs.getDouble(7));
contadorTmp++;
}
} catch (SQLException errorSQL) { errorSQL.printStackTrace(); }
finally {
try { if (rs != null) rs.close(); } catch (Exception errorRS) { errorRS.printStackTrace(); }
try { if (st != null) st.close(); } catch (Exception errorST) { errorST.printStackTrace(); }
try { if (conn != null) conn.close(); } catch (Exception errorCONN) { errorCONN.printStackTrace(); }
}
DefaultTableModel model = new DefaultTableModel(filas, columnas);
table.setModel(model);
I have tried to investigate a way to set a Width to each column in this case but, I can not find anything.
Any help will be welcome.