How to add horizontal/vertical scrollbars to JScrollPane

回眸只為那壹抹淺笑 提交于 2019-12-31 02:49:11

问题


I want to add horizontal scrollbars to my JScrollPane, because my table right now looks like the one below:

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:

  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!


回答1:


You have to set for JTable#setAutoResizeMode, more is described in JTable's tutorial Setting and Changing Column Widths




回答2:


try

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


来源:https://stackoverflow.com/questions/9164786/how-to-add-horizontal-vertical-scrollbars-to-jscrollpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!