简体   繁体   English

没有滚动窗格的NetBeans JTable,保留标题

[英]NetBeans JTable without Scroll Pane, Keep Header

I'm attempting to add a JTable with NetBeans GUI builder. 我正在尝试使用NetBeans GUI构建器添加JTable。 The table is inside a panel which already has a scroll bar. 该表位于已经具有滚动条的面板内。 Netbeans automatically creates all JTables inside of a JScrollPane. Netbeans自动在JScrollPane中创建所有JTable。

However, I want the table to scroll as part of a larger page. 但是,我希望表格作为更大页面的一部分滚动。 I do not need two scroll bars. 我不需要两个滚动条。

My problem is: if I get rid of the scroll pane, I lose the header. 我的问题是:如果摆脱滚动窗格,则会丢失标题。

Is there a way to have a table with a header inside the Netbeans GUI builder? 在Netbeans GUI构建器中是否可以使用带有标题的表?

My problem is: if I get rid of the scroll pane, I lose the header. 我的问题是:如果摆脱滚动窗格,则会丢失标题。

  • JTableHeader is (automatically) visible in the case that JTable is inside JScrollPane 如果JTableJScrollPane内部,则JTableHeader是(自动)可见的

  • you have to get JTableHeader from JTable and place this Object programatically by using LayoutManager to the container, I'm strongly recommend to use BorderLayout or GridBagLayout for this container 您必须从JTable获取JTableHeader并通过使用LayoutManager将该对象编程放置到容器中,强烈建议对此容器使用BorderLayoutGridBagLayout

If you add JTabel directly to container(not to JScrollPane ) you need to add JTableHeader by yourself(programatically ), try next example: 如果将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);
}

在此处输入图片说明

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

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