Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable with horizontal scrollbar

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?

like image 427
Mr CooL Avatar asked Mar 16 '10 07:03

Mr CooL


People also ask

How to add scroll bar in JTable in Java?

To create a scrollable JTable component we have to use a JScrollPane as the container of the JTable . Besides, that we also need to set the table auto resize mode to JTable. AUTO_RESIZE_OFF so that a horizontal scroll bar displayed by the scroll pane when needed.


2 Answers

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); 
like image 62
Romain Linsolas Avatar answered Oct 03 '22 21:10

Romain Linsolas


Set the AutoResizeMode to OFF in the properties of the jTable

like image 39
Nitesh Verma Avatar answered Oct 03 '22 23:10

Nitesh Verma