How to show a horizontal scroll bar when a table column is not large enough

I use a JTable (associated to a default table model and a scroll pane). It only has one column - looks like a list. When the column is not large enough to display the cell content, it will display "..." at the end of a cell.

But I want a horizontal scroll bar at this moment. How to implement it?

I checked the dicussions about "horizontal scroll" in this forum. Almost all the topics are talking about "PreferredScrollableViewportSize vs. PreferredSize". However, I am talking about "cell content width vs. cell display width". I expect a horizontal scroll bar when cell content is not entirely displayed.

Any hints?

[642 byte] By [princesuna] at [2007-10-2 16:29:01]
# 1
I'ver never done this, but you might try a custom renderer: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#renderer-Puce
Pucea at 2007-7-13 17:30:26 > top of Java-index,Desktop,Core GUI APIs...
# 2
Take a look at the thread below. If you resize your column to fit the contents, you'll get the scrollbar. http://forum.java.sun.com/thread.jspa?forumID=54&messageID=2532529&threadID=527301
JayDSa at 2007-7-13 17:30:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

JayDS,

You recommended thread is an implementation of "autofit table columns".

To display the horizontal scroll bar , I need to compare the initial table width with the autofit table width. However, I got a strange problem:

After I create a table, I call table.getWidth(). It returns 0, which makes puzzled.

Anything I did wrong?

BTW, Puce, I read the online document, didn't find the tips. Anyway, thanks!

princesuna at 2007-7-13 17:30:26 > top of Java-index,Desktop,Core GUI APIs...
# 4
JTable.AUTO_RESIZE_OFF is what you want!
javakicksassa at 2007-7-13 17:30:26 > top of Java-index,Desktop,Core GUI APIs...
# 5

>> JTable.AUTO_RESIZE_OFF is what you want!

Yes, it's a popular solution. But in my case, it doesn't work properly.

my JTalbe has one column without column header.

table = new JTable(defaultTableModel);

table.setPreferredScrollableViewportSize(new Dimension(visibleTableWidth, visibleTableHeight));

table.setPreferredSize(new Dimension(autofitColumnWidth, actuallyHeight); // parameters depend on the length of cell content and number of rows

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

JScrollPane scroll=new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JSCrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

Each time when "TableChanged", the autofitColumnWidth may change, so I will call table.setPreferredSize again.

My problem is: The horizontal scrollbar will appear when the column is wide enough. This is okay. However, after that, it never disappears even the column gets very narrow.

princesuna at 2007-7-13 17:30:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

>>>>

My problem is: The horizontal scrollbar will appear when the column is wide enough. This is okay. However, after that, it never disappears even the column gets very narrow.

Now, it's okay. Horizontal scrollbar can appear or disappear as needed.

But, a new problem comes up - after horizontal scrollbar disappears, it leaves a shadow in the position which originally occupied by the bar. Don't know why. Anybody knows?

princesuna at 2007-7-13 17:30:26 > top of Java-index,Desktop,Core GUI APIs...