简体   繁体   English

如何将水平/垂直滚动条添加到JScrollPane

[英]How to add horizontal/vertical scrollbars to JScrollPane

I want to add horizontal scrollbars to my JScrollPane, because my table right now looks like the one below: 我想将水平滚动条添加到我的JScrollPane中,因为现在我的表如下图所示:

在此处输入图片说明

Here is my code to create the table: 这是我创建表的代码:

  this.table = new JTable();
  this.table.setShowGrid(false);
  this.table.getTableHeader().setFont(ReportViewConstants.TABLE_FONT);
  this.table.setFont(ReportViewConstants.TABLE_FONT);

  this.scrollPane = new JScrollPane(this.table);
  Dimension size = new Dimension(300, 400);
  this.scrollPane.setPreferredSize(size);
  this.scrollPane.setMinimumSize(size);
  this.scrollPane.getViewport().setBackground(Color.WHITE);

Can you point out what I'm doing wrong? 您能指出我做错了什么吗? When I changed the line to create a JScrollPane to: 当我更改该行以创建JScrollPane到:

  this.scrollPane = new JScrollPane(this.table,
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

The scrollbars are there, but I still cannot scroll horizontally. 滚动条在那里,但是我仍然不能水平滚动。 See the screenshot below. 请参见下面的屏幕截图。

在此处输入图片说明

Thanks! 谢谢!

您必须为JTable#setAutoResizeMode进行设置,有关更多信息 ,请参见JTable's教程“ 设置和更改列宽”

try 尝试

Dimension tableSize = new Dimension(500, 400);
this.table.setPreferredSize(tableSize);

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

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