0

I have a JXTable that is created like this:

JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane, BorderLayout.CENTER);
tbl = new JXTable();
tbl.setColumnSelectionAllowed(true);
tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tbl.setEditable(false);
scrollPane.setViewportView(tbl);

The problem is that when it gets filled, all the columns are squashed to fit into the table. I expected the columns to be adjusted according to the width of the column header or column value (which ever is broader) and then for a horizontal scroll bar to be displayed that will enable me to scroll left or right.

How do I bridge this problem?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rico Strydom
  • 537
  • 1
  • 6
  • 26
  • Start by trying to use [`JXTable#setAutoResizeMode`](http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setAutoResizeMode(int)) to `JTable.AUTO_RESIZE_OFF` and then have a look at [this example](http://stackoverflow.com/questions/13013989/how-to-adjust-jtable-columns-to-fit-the-longest-content-in-column-cells/13037771#13037771). This is a very expensive operation, so beware... – MadProgrammer Jul 10 '14 at 05:17
  • setHorizontalScrollEnabled(true) did the trick. – Rico Strydom Jul 10 '14 at 05:33
  • 1
    [crossposted](http://www.coderanch.com/t/636348/GUI/java/JXTable-width-adjust-content) – mKorbel Jul 10 '14 at 07:22

1 Answers1

1

You should use the packAll method: this resizes columns to fit the viewport; if horizontal scrolling is enabled, columns will get their preferred width.

dawww
  • 371
  • 1
  • 4
  • 16