简体   繁体   English

来自Resultset的JTable无法垂直滚动

[英]JTable from Resultset not scrolling vertically

I'm having trouble with a JTable component for more than a week now. 我在一个JTable组件上遇到了一个多星期的麻烦。 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): 在JInternalFrame表单内部,我有一个这样创建的JTable(在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()). tblData是从正确加载的ResultSet填充的(数据已通过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. 先感谢您。

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. 基本上,使用setPreferred/Minimum/MaximumSize将会导致你的问题,你也应该用谨慎setPreferredScrollableViewportSize和不应该使用组件的preferredSize ,因为这将使得滚动窗格希望的大小分量相同。

You could also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? 您还可以看看是否应该避免在Java Swing中使用set(Preferred | Maximum | Minimum)Size方法?

I know that perhaps this is not the "best" solution, but out of despair I tried it and it worked: 我知道这可能不是“最佳”解决方案,但出于绝望,我尝试了一下并成功了:

  • Simply I deleted the jTable and jScrollPane components from the form, and re-created them only this time I did not change anything but the size of the controls to fit my needs; 我只是简单地从表单中删除了jTable和jScrollPane组件,只是这次只重新创建了它们,除了控件的大小可以满足我的需要外,其他所有操作都没有。 no other option tampered with. 没有其他选择被篡改。

And this time it worked just as expected. 这次它按预期运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM