JTable with horizontal scrollbar

Mr CooL picture Mr CooL · Mar 16, 2010 · Viewed 93.6k times · Source

Is there any way to enable horizontal scroll-bar whenever necessary?

The situation was as such: I've a JTable, one of the cells, stored a long length of data. Hence, I need to have horizontal scroll-bar.

Anyone has idea on this?

Answer

Romain Linsolas picture Romain Linsolas · Mar 16, 2010

First, add your JTable inside a JScrollPane and set the policy for the existence of scrollbars:

new JScrollPane(myTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

Then, indicate that your JTable must not auto-resize the columns by setting the AUTO_RESIZE_OFF mode:

myJTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);