jTable 没有显示水平滚动旋钮

jTable didn't show knob to scroll horizontal

我在这两个程序上使用了一种简单且相同的技术。

左图有旋钮,右图没有旋钮:

但是,为什么第二个程序(右边)没有像第一个程序(左边)那样在卷轴上有一个旋钮? 第二个程序也有很多数据,我需要旋钮向下滚动。 我已经尝试查看这两个程序的区别,但没有找到一个。

这是我的代码:

 public UIForecasting() {
    initComponents();
    jTable2.setModel(new DefaultTableModel());
}
public void showdata() {
    DefaultTableModel tabelawal = new DefaultTableModel();
    tabelawal.addColumn("ID");
    tabelawal.addColumn("Jumlah Pemakaian");
    tabelawal.addColumn("Nilai Pemulusan Eksponensial");
    tabelawal.addColumn("Nilai Trend");
    tabelawal.addColumn("Hasil Peramalan");
    tabelawal.addColumn("MBA");
    tabelawal.addColumn("MSE");
    try {
        koneksi();
        sql = "SELECT * FROM tabelforecasting";
        Statement stat = conn.createStatement();
        ResultSet res = stat.executeQuery(sql);

        while (res.next()) {
            tabelawal.addRow(new Object[]{""+ res.getString(1), res.getInt(2), res.getDouble(3), res.getDouble(4), res.getDouble(5),res.getDouble(6),res.getDouble(7)});

        }

        jTable2.setModel(tabelawal);            
    } catch (Exception e) {
    }
}

您的代码中未显示(除其他外)是您对 JScrollPane 的构造。可能是这样的:

JScrollPane scroller = new JScrollPane(jTable2);

这使用 1 个参数 JScrollPane constructor,它具有滚动条的默认策略:

Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.

如果你总是想看到滚动条,你应该使用其他构造函数之一,或者调用 setVerticalScrollBarPolicy() and/or setHorizontalScrollBarPolicy() 并传入常量 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS.