Re: Horizontally scrolling JTable



Andrew Thompson wrote:
RedGrittyBrick wrote:
Andrew Thompson wrote:
RedGrittyBrick wrote: ..

Thanks, that is perfect in all but one respect ... Unfortunately
the table headings no longer show.

I am beginning to think this is a bug, and have started trawling
through the bug D/B looking for similar problems.

Here is a likely looking one, though Sun
marked it 'Closed, not a bug' (shrugs). *
<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4194022>

using a slight variant of the advice
on that page, I came up with this (very
hackish looking) code that seems to behave
as we might expect..


<snip SSCCE head>

HorizScrollTable() {
setLayout(new BorderLayout());
JTable table = new JTable(data, headings){
Component parent;

public boolean getScrollableTracksViewportWidth(){
return isThinnerThanParent();
}

boolean isThinnerThanParent() {
if (parent==null) {
parent = getParent();
}
Dimension d1 = getSize();
Dimension d2 = parent.getSize();

return (d1.width<=d2.width);
}
};
table.setAutoResizeMode(
JTable.AUTO_RESIZE_ALL_COLUMNS);
JScrollPane scrollPane = new JScrollPane(table);

add(scrollPane, BorderLayout.CENTER);
}

<snip SSCCE tail>

Does that work for you?

Thanks Andrew, yes that works the way I wanted.

Before I read your latest posting I'd also been hunting in the bugs database as you suggested ...

Searching Sun's Bug database led me (eventually) to Bug 4127936 reported 10-APR-1998 and marked "In progress, bug", "Evaluation: ... needs to be fixed before FCS with JDK1.2." :-o

There are several suggested workarounds in the commentary. For future readers of this thread: apart the workaround which Andrew found (see above), another workaround that I tried was ...

....
scrollPane.addComponentListener(new
CorrectStrangeBehaviourListener(table, scrollPane));
....


/**
*
* @author dbeutner 16-MAY-2002
* See Sun Java bug report 4127936.
* Amended 29/3/2007 by RedGrittyBrick to add
* constructor with parameters
*
*/
private class CorrectStrangeBehaviourListener
extends ComponentAdapter {

private JTable table;

private JScrollPane scrollPane;

CorrectStrangeBehaviourListener(JTable table,
JScrollPane scrollPane) {
this.table = table;
this.scrollPane = scrollPane;
}

public void componentResized(ComponentEvent e) {
if (table.getPreferredSize().width
<= scrollPane.getViewport()
.getExtentSize().width) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
} else {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
}
}

The one Andrew found is shorter but since I have several tables it may be worth me putting the bulk of the workaround code into a separate class as the above example does.

Thanks again Andrew.
.



Relevant Pages

  • Re: 2.6.12-rc1-mm3
    ... > Hi Andrew, ... After booting with "kdb=early" I found out that the kernel blocks with the ... and BUG() is called. ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Let us set the record staight Steve Two names
    ... I posted what you said about Andrew's wife as best I could from ... Andrew dealing with me, he does his usuall wimp routine and goes to MAtt. ... And if I bug YOU, two names, so be it. ... I also thank Berman for informing me about Shade ...
    (alt.vacation.las-vegas)
  • Re: launch applet with APPLET or PLUGIN
    ... > fails in my Firefox, but the next one, with the suspicious semicolon ... Now I have something to report to the Tomcat people. ... ...does seem like a bug. ... Andrew Thompson ...
    (comp.lang.java.programmer)
  • Re: [MSH] Document bug - Snapins Help File
    ... Thanks Andrew. ... I opened a bug for it internally. ... non-breaking Name change issues (like the old icon etc). ...
    (microsoft.public.windows.server.scripting)
  • Re: Impossibly Slow Load-Time for Cross-Reference Window Items
    ... > fixed in SP1. ... > Clive's Word 2001 Workaround: ... > with MacWord than the reload bug itself. ... >> to go through before I can clear an update for installation on the ...
    (microsoft.public.mac.office.word)