简体   繁体   中英

Java Swing; Problems with Horizontal Scroll for a JTable embedded in a JScrolledPane

I followed some tips in this guide: JTable with horizontal scrollbar but still having problems.

I have two column headers: Name and Description (for our purposes in this ex.)

I want to be able to scroll horizontally whenever an entry is added (ie: Name/Description is just 1 line). However, Swing doesn't seem to default to that behavior!

I have a JTable embedded in a ScrollPane. JScrollPane has the following parameters: JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED and JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED

If I disable with: myTable.AutoResizeMode(AUTO_RESIZE_OFF) the column headers don't fill up the whole table (maybe just 1/2 of the table). I have to manually resize the "Description" Column to see the whole thing.

How can I have autoresizing, but the horizontal scrolling still works?

One approach might be to use setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS) when you create the table and then use setPreferredScrollableViewportSize() to specify your desired size. After you pack() the window, use setAutoResizeMode(JTable.AUTO_RESIZE_OFF) to allow horizontal scrolling.

You can extend JTable as follows:

public class JHorizontalFriendlyTable extends JTable {

  @Override
  public Dimension getPreferredSize() {
    if (getParent () instanceof JViewport) {
      if (
        ((JViewport) getParent()).getWidth() > super.getPreferredSize().width)
      ) {
        return getMinimumSize();
      }
    }
    return super.getPreferredSize(); 
  }

  @Override
  public boolean getScrollableTracksViewportWidth () {
    if (autoResizeMode != AUTO_RESIZE_OFF) {
      if (getParent() instanceof JViewport) {
        return (((JViewport) getParent()).getWidth() > getPreferredSize().width);
      }
      return true;
    }
    return false;
  }
}

sample usage:

jScrollPane1 = new JScrollPane();
TableModel jTable1Model = new DefaultTableModel(...); 
JTable jTable1 = new JHorizontalFriendlyTable();
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setViewPortView(jTable1);
jTable1.setModel(jTable1Model);
jTable1.setPreferredSize(new java.awt.Dimension(1051,518));
jTable1.setPreferredScrollableViewPortSize(new java.awt.Dimension(1000,528));
jTable1.getSize(new java.awt.Dimension(1051, 528));

if (jTable1.getPreferredScrollableViewPortSize().getWidth() > 
  ((JViewPort) jTable1.getParent()).getPreferredSize().getWidth())
  {
  jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  jTable1.doLayout();
}

jTable1.setDragEnabled(false);
jTable1.setColumnSelectionAllowed(false);
jTable1.getTableHeader().setReorderingAllowed(false);

Auto resize means automatically resizing to avoid scrolling.

So the answer is no with standard JTable .

Check out JXTable and see if it does what you want (I haven't used it myself).

If I disable with: myTable.AutoResizeMode(AUTO_RESIZE_OFF) the column headers don't fill up the whole table (maybe just 1/2 of the table).

By default each column is only 75 pixels wide. You can change the width of the description column to be whatever you want. Read the JTable API and you will find a link to the Swing tutorial on "How to Use Tables" which explains how to do this.

However, even using this approach the column size will not change if the user resizes the frame. It you want to handle this situation then you need to add a ComponentListener to the table (or maybe the scroll pane). Then whenever the componentResized() method is fired you can total the widths of the two columns and then adjust the width of the description column as required to make sure the column total width is not less than the width of the table.

I have to manually resize the "Description" Column to see the whole thing.

If you know that your description column will always be large enough to fill the viewport of the scrollpane then you can use the Table Column Adjuster to automatically calculate the maximum width of the description column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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