1
I am developing a basic program in Java Application (Desktop) that performs a simple registration and displays on another screen through a Jtable the recorded data in the same.
My problem is that there are 24 columns to be displayed in this table, so I cannot view all the data as needed.
Below is the images demonstrating how the table looks.
Note: The individual size of the columns changes (Increases one, decreases the others automatically), but the total size of the column is unchanged.
Below is the code of this screen:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package view;
import controller.CadInternoJpaController;
import java.util.Iterator;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import model.CadInterno;
/**
*
* @author Casa
*/
public class JFAltExc extends javax.swing.JFrame {
/**
* Creates new form JFAltExc
*/
public JFAltExc() {
initComponents();
readJTable();
}
/**
*
*/
public void readJTable(){
DefaultTableModel tableModel = (DefaultTableModel) jtCadInterno.getModel();
tableModel.setNumRows(0);
EntityManagerFactory objFactory = 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’ve set a size of 500 for each field, with the intention of testing, making all fields very large and making room for horizontal scrolling, allowing you to view the contents of all fields, however as it IS NOT WORKING and size does not change, it divides into a smaller size but equaling the size of all fields.
Anything I can post more details.
Try this: https://answall.com/questions/165066/comoros
– user28595
I tried, but the fields that did not fit on the screen simply disappeared, but the ones that stayed, was displayed in the correct size.
– Lucas H. Rosa
Lucas, this code is not testable. You can edit with a table containing some lines with sample data?
– user28595
I’m sorry, but I don’t understand what you need me to do, could you please explain. Thank you :)
– Lucas H. Rosa
There are dependencies in your code. You need connection to your bank, which is impossible. Remove the dependencies of entitymanager and your database, and add 2 or 3 lines manually to serve as an example.
– user28595
@diegofm Funny, as requested, I was creating a trial version, without communication with the database, but it worked correctly. Does it have to do with the process of calling the data to the table?
– Lucas H. Rosa