Display multiple columns [closed]

1

I am developing a basic program in Java Application (Desktop) that performs a simple registration and displays in another screen through a JTable the data recorded in it.

My problem is that there are 24 columns to display in this table, so I can not see all the data as needed.

Below are the images showing how the table is.

Note: The individual size of the columns change (Increases one, decreases others automatically), but the total size of the columns is unchanged.

Hereisthecodeforthisscreen:

/**Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties.*Tochangethistemplatefile,chooseTools|Templates*andopenthetemplateintheeditor.*/packageview;importcontroller.CadInternoJpaController;importjava.util.Iterator;importjavax.persistence.EntityManager;importjavax.persistence.EntityManagerFactory;importjavax.persistence.Persistence;importjavax.swing.JTable;importjavax.swing.table.DefaultTableModel;importmodel.CadInterno;/****@authorCasa*/publicclassJFAltExcextendsjavax.swing.JFrame{/***CreatesnewformJFAltExc*/publicJFAltExc(){initComponents();readJTable();}/****/publicvoidreadJTable(){DefaultTableModeltableModel=(DefaultTableModel)jtCadInterno.getModel();tableModel.setNumRows(0);EntityManagerFactoryobjFactory=Persistence.createEntityManagerFactory("CERVPU");
        EntityManager manager = objFactory.createEntityManager();
        CadInternoJpaController jpa = new CadInternoJpaController(objFactory);
        try{
            for (Iterator<CadInterno> it = jpa.findCadInternoEntities().iterator(); it.hasNext();) {
                CadInterno c = it.next();
                tableModel.addRow(new Object[]{
                    c.getCodigo(),
                    c.getDataEntrada(),
                    c.getDataSaida(),
                    c.getNome(),
                    c.getEndereco(),
                    c.getNumero(),
                    c.getComplemento(),
                    c.getBairro(),
                    c.getCidade(),
                    c.getEstado(),
                    c.getPai(),
                    c.getMae(),
                    c.getDataNasc(),
                    c.getRg(),
                    c.getCpf(),
                    c.getGrauEscolar(),
                    c.getTelefone(),
                    c.getCelular(),
                    c.getEstCivil(),
                    c.getTiposDrogas(),
                    c.getTentParar(),
                    c.getDetParar(),
                    c.getEstPreso(),
                    c.getMotPreso(),
                    c.getDataCriacao()
                });
            }
        } catch (Exception ex)
        {
            //Logger.getLogger(AplicaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }




        jtCadInterno.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        jtCadInterno.getColumnModel().getColumn(0).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(1).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(2).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(3).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(4).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(5).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(6).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(7).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(8).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(9).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(10).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(11).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(12).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(13).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(14).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(15).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(16).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(17).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(18).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(19).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(20).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(21).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(22).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(23).setPreferredWidth(500);
        jtCadInterno.getColumnModel().getColumn(24).setPreferredWidth(500);

        jtCadInterno.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtCadInterno = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jtCadInterno.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Data Entrada", "Data Saida", "Nome", "Endereço", "Número", "Complemento", "Bairro", "Cidade", "Estado", "Nome Pai", "Nome Mãe", "Nascido em", "RG", "CPF", "Grau Escolar", "Telefone", "Celular", "Estado Civil", "Drogas Usadas", "Tentou Parar?", "Detalhe de Como Tentou Parar", "Esteve Preso?", "Motivo Pelo Qual Foi Preso", "Cadastrado em"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtCadInterno.setMaximumSize(new java.awt.Dimension(1272, 503));
        jtCadInterno.setMinimumSize(new java.awt.Dimension(1272, 503));
        jtCadInterno.setPreferredSize(new java.awt.Dimension(1272, 503));
        jScrollPane1.setViewportView(jtCadInterno);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(89, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFAltExc.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> {
            new JFAltExc().setVisible(true);
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtCadInterno;
    // End of variables declaration                   
}

If you look, I set a size of 500 for each field, with the intent of testing, making all fields look great and open space for horizontal scrolling, allowing you to view the contents of all fields, but how does it It is NOT RUNNING and the size does not change, it splits into a smaller size but equaling the size of all fields.

Anything I can post more details.

    
asked by anonymous 19.07.2017 / 17:49

1 answer

1

You have put the jtCadInterno.setAutoResizeMode (JTable.AUTO_RESIZE_OFF); 2x in the code, well delete the first one and change the low one to "AUTO_RESIZE_ALL_COLUMNS"

You can also do this:

.getColumn (0) .setPreferredWidth (100); // Conventional column size .getColumn (0) .setMaxWidth (500); // Maximum size it can reach

    
19.07.2017 / 18:08