简体   繁体   English

JScrollPane不属于JTable

[英]JScrollPane doesn't belong to JTable

I added a JScrollPane for my JTable. 我为我的JTable添加了JScrollPane。 It showed up, but not scrollable. 它出现了,但不能滚动。

public class AddDepartment extends JFrame {
    private static JPanel contentPane;
    private Connection DBConnection;
    static Connection conn = new DBConnection().connect();
    static PreparedStatement pstmt = null;
    static ResultSet rs = null;
    static DefaultTableModel model = null;
    private static JTable tableDepartments;
    private static JScrollPane scrollPane;

    public AddDepartment() throws SQLException {
        model = new DefaultTableModel();
        tableDepartments = new JTable(model);
        tableDepartments.setRowSelectionAllowed(false);
        tableDepartments.setEnabled(false);
        tableDepartments.setSize(new Dimension(197, 66));
        tableDepartments.setLocation(161, 34);
        tableDepartments.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        updateDepartments();
    }

    private static void updateDepartments() throws SQLException {
        try {
            String sql = "SELECT Name FROM Departments";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            model.addColumn("Name");
            while (rs.next()) {
                String departmentName = rs.getString("Name");
                model.addRow(new Object[] { departmentName });
            }
            if (tableDepartments.getRowCount() > 5) {
                scrollPane = new JScrollPane(tableDepartments);
                scrollPane.setSize(new Dimension(10, 50));
                scrollPane.setLocation(360, 34);
                scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                contentPane.add(scrollPane);
            }
            contentPane.add(tableDepartments);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        } finally {
            rs.close();
            pstmt.close();
        }
    }
}

Departments table: 部门表:

部门表

看起来像这样

Above picture shows how it looks like. 上图显示了它的外观。 I cannot scrolldown. 我无法向下滚动。

By constraining your JTable size: 通过限制JTable的大小:

tableDepartments.setSize(new Dimension(197, 66));

you prevent it from sizing as needed and thus prevent it from scrolling. 您可以防止按需调整大小,从而防止滚动。 Don't do this, but instead let it expand to its desired size. 不要这样做,而是让它扩展到所需的大小。 If this doesn't fix your problem, then you will need to create and post a valid sscce , a program that has no database code, no code not relevant to the problem and that demonstrates your problem for us. 如果这不能解决您的问题,那么您将需要创建并发布有效的sscce ,该程序没有数据库代码,没有与问题无关的代码,并且可以为我们演示您的问题。

Other issues include a gross over-use of the static modifier, and your setting absolute sizes and positions (and likely use of null layouts) but that's for another question 其他问题包括过度使用static修饰符,以及您设置的绝对大小和位置(以及可能使用空布局),但这是另一个问题


Edit 编辑
You're adding your JTable twice to the GUI, once to the JScrollPane (good) and again to the contentPane (bad), and this is messing you up since you can only add a component to one container. 您将JTable 两次添加到GUI,一次添加到JScrollPane(好的),又一次添加到contentPane(坏的),由于您只能将一个组件添加到一个容器,所以这很麻烦。 The 2nd add is removing the jtable from the jscrollpane. 第二个添加是从jscrollpane中删除jtable。 Solution: don't do this. 解决方案:不要这样做。 Add the jscrollpane to the contentPane, not the JTable. 将jscrollpane添加到contentPane, 而不是 JTable。

So get rid of: 因此摆脱:

contentPane.add(tableDepartments);

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

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