【问题标题】:Java, GirdLayout, ScrollPane not workingJava、GridLayout、JScrollPane 不工作
【发布时间】:2017-06-02 08:21:28
【问题描述】:

我有两个 JTables,每个 JTables 都被添加到一个带有 Borderlayout 的 Container(TableHeader 到 BoderLayout.PAGE_START 和 JTable 到 Center)这些 Container 被添加到一个 JScrollPane。

将两个Container添加到一个带有Gridlayout的Container中,该Gridlayout添加到JPanel的中心。

问题是,滚动条没有显示,或者如果强制滚动条一直显示,它就不起作用。 JTable 有超过 100 个条目,但只显示前约 30 个条目。

我已经尝试搜索并找到了一些可能的修复方法,但它们没有奏效。我已经尝试过使用首选大小,向 ScrollPane 添加了布局以及其他一些可能性,但没有任何效果。

Jtable的部分代码:

        this.locations = new JTable(data, columnNames);
        this.locations.getTableHeader().setReorderingAllowed(false);
        this.locations.setDefaultEditor(Object.class, null);
//      this.locations.setPreferredSize(new Dimension(100, 100));

        Container table = new Container();
        table.setLayout(new BorderLayout());
        table.add(this.locations.getTableHeader(), BorderLayout.PAGE_START);
        table.add(this.locations, BorderLayout.CENTER);

        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.content.add(scrollPane);

Locations 是 JTable,Content 是带有 Gridlayout 的 Container,然后将其添加到带有 BorderLayout 的 JPanel。

【问题讨论】:

  • 请添加一个完整的、可编译的代码示例。

标签: java swing jscrollpane grid-layout jscrollbar


【解决方案1】:
    Container table = new Container();
    table.setLayout(new BorderLayout());
    table.add(this.locations.getTableHeader(), BorderLayout.PAGE_START);
    table.add(this.locations, BorderLayout.CENTER);

上面的代码不需要了。

你只需要:

    this.locations = new JTable(data, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    this.content.add(scrollPane);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    相关资源
    最近更新 更多