Re: Horizontally scrolling JTable
- From: RedGrittyBrick <redgrittybrick@xxxxxxxxxxxxx>
- Date: Fri, 30 Mar 2007 10:56:26 +0100
Bart Cremers wrote:
RedGrittyBrick wrote:Andrew Thompson wrote:RedGrittyBrick wrote:
I am unable to make my JTable/JScrollPane so that it gains horizontal
scrollbars when container width < JTable min width.
Any suggestions?
SSCCE
...
JPanel p = new JPanel(new BorderLayout());// new ..
p.add(table, BorderLayout.CENTER); // [3]
p.setPreferredSize( table.getPreferredSize() );
JScrollPane scrollPane = new JScrollPane( p );
...
Thanks, that is perfect in all but one respect ...
Unfortunately the table headings no longer show.
In case I've misread your message, here is my revised constructor:
HorizScrollTable() {
setLayout(new BorderLayout());
JTable table = new JTable(data, headings); // headings!
for (int i = 0; i < headings.length; i++) {
TableColumn column = table.getColumnModel().getColumn(i);
int width = widths[i];
column.setPreferredWidth(width);
column.setMinWidth(width);
}
JPanel p = new JPanel(new BorderLayout());
p.add(table, BorderLayout.CENTER);
p.setPreferredSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane(p);
add(scrollPane, BorderLayout.CENTER);
}
(new readers please see first posting in thread for original SSCCE in full)
Is it impossible to have horizontal scrolling AND headings?
By setting the header explicitly on the scrollPane you can have
headers:
scrollPane.setColumnHeaderView(table.getTableHeader());
Thanks Bart, that works well.
I guess introducing a JPanel between JTable and JScrollPane still counts as a workaround for bug 4127936, but it is the most unobtrusive workaround of the ones I've tried. I'm not sure if workarounds in source code for Swing bugs should be subtle and unobtrusive or big and obvious.
My thoughts are moving towards unobtrusive but with a big comment.
.
- References:
- Horizontally scrolling JTable
- From: RedGrittyBrick
- Re: Horizontally scrolling JTable
- From: Andrew Thompson
- Re: Horizontally scrolling JTable
- From: RedGrittyBrick
- Re: Horizontally scrolling JTable
- From: Bart Cremers
- Horizontally scrolling JTable
- Prev by Date: Re: SWT without Eclipse?
- Next by Date: J2SE 1.4 Solaris window always on top
- Previous by thread: Re: Horizontally scrolling JTable
- Next by thread: Undecorated JFrame versus JWindow
- Index(es):