Search code examples
javaswingjtablejscrollpanetablemodel

JTable from Resultset not scrolling vertically


I'm having trouble with a JTable component for more than a week now. Hope you can point me in the right direction.

Inside a JInternalFrame form, I have a JTable created like so (Inside the auto-generated code from NetBeans):

    jScrollPane1 = new javax.swing.JScrollPane();
    tblData = new javax.swing.JTable();
    jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane1.setMaximumSize(new java.awt.Dimension(468, 167));
    jScrollPane1.setMinimumSize(new java.awt.Dimension(468, 167));
    jScrollPane1.setPreferredSize(new java.awt.Dimension(453, 167));

    tblData.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
    tblData.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "ID Empresa", "Código Empresa", "Nombre de la Empresa"
        }
    ));
    tblData.setFillsViewportHeight(true);
    tblData.setIntercellSpacing(new java.awt.Dimension(5, 5));
    tblData.setMaximumSize(new java.awt.Dimension(300, 100));
    tblData.setMinimumSize(new java.awt.Dimension(300, 100));
    tblData.setName(""); // NOI18N
    tblData.setPreferredSize(new java.awt.Dimension(300, 100));
    tblData.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    jScrollPane1.setViewportView(tblData);

The table tblData is populated from a ResultSet that loads correctly (data has been verified via System.out.println()). Now, the thing is that once the data has been retrieved, a function is called to "refresh" the table as follows:

private void prepareTable(boolean isEmpty) {
    String[] colHeadings = {"ID Empresa","Código Empresa","Nombre de la Empresa"};

    DefaultTableModel model = new DefaultTableModel();
  tblData.setPreferredScrollableViewportSize(tblData.getPreferredSize());

    try {
        if (isEmpty) {
            model.setColumnIdentifiers(colHeadings);
            tblData.setModel(model);
        } else {
            Verbo.rs.beforeFirst();

            tblData.setModel(DbUtils.resultSetToTableModel(Verbo.rs));
        }
    } catch (SQLException se) {

    }
    ((DefaultTableModel) tblData.getModel()).fireTableStructureChanged();

}

Thing is that, although the rows are retrieved and loaded to the table model, the vertical scroll bar for the table does not work even though it is shown, and therefore just the first records are shown.

Is there anything visible wrong with what I'm doing? Please help me for I have searched and all answers here and in other sites have not been useful.

Thank you in advance.


Solution

  • Basically, using setPreferred/Minimum/MaximumSize is going to cause you issues, you should also be careful using setPreferredScrollableViewportSize and should never use the component's preferredSize as this will make the scroll pane want to be the same size as the component.

    You could also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?