有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NetBeans JTable不带滚动窗格,保留标题

我正在尝试使用NetBeans GUI builder添加一个JTable。桌子在一个已经有滚动条的面板内。Netbeans会在JScrollPane中自动创建所有JTables

但是,我希望表格作为更大页面的一部分滚动。我不需要两个滚动条

我的问题是:如果我去掉滚动窗格,我会丢失标题

有没有办法在Netbeans GUI builder中创建一个带有标题的表


共 (2) 个答案

  1. # 1 楼答案

    My problem is: if I get rid of the scroll pane, I lose the header.

    • 如果JTableJScrollPane内,则JTableHeader是(自动)可见的

    • 您必须从JTable获取JTableHeader,并通过使用LayoutManager以编程方式将此对象放置到容器中,我强烈建议对此容器使用BorderLayoutGridBagLayout

  2. # 2 楼答案

    如果直接将JTabel添加到容器中(而不是添加到JScrollPane),则需要自己(以编程方式)添加JTableHeader,请尝试下一个示例:

    public static void main(String[] args) {
        JTable t = new JTable(new Object[][]{{1,2,3}},new Object[]{"1","2","3"});
        JFrame frame = new JFrame();
        frame.add(t.getTableHeader(),BorderLayout.NORTH);
        frame.add(t);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    

    enter image description here