tags:

views:

111

answers:

4

I followed some tips in this guide: http://stackoverflow.com/questions/2452694/jtable-horizontal-scrollbar-in-java 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?

+1  A: 

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).

Geoffrey Zheng
+2  A: 

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.

trashgod
+1  A: 

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

camickr
+1 This is a more general solution than the accepted answer.
trashgod
+1  A: 

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()).width() > getPreferredSize().width);
      }
    }
    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);
eee