Set a Width to the JTable columns in Java

0

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.

    
asked by Robert Gomez 27.11.2016 в 18:13
source

2 answers

1

Find it by its identifier. For example for the second column called Type would be:

table.getColumn("Tipo").setPreferredWidth(30);

This method uses equals to see if it matches some identifier, according to the java documentation , passed in the object columnas .

    
answered by 27.11.2016 / 18:30
source
0

I would use the method setPreferredWidth()

A serious way

TableColumn columna = null;
//se hace el recorrido de tu arreglo de columnas
for (int i = 0; i < columna.length; i++) {
    columna = table.getColumnModel().getColumn(i);
    swith(i){
        case 0:
            columna.setPreferredWidth(50); //el ancho de la column
            break;
        case 1:
            columna.setPreferredWidth(80);
            break;
        //etc aqui las demas columnas
    }

}    
    
answered by 27.11.2016 в 18:35