Re: Horizontally scrolling JTable



On Mar 28, 3:27 pm, RedGrittyBrick <redgrittybr...@xxxxxxxxxxxxx>
wrote:
Andrew Thompson wrote:
On Mar 28, 7:17 pm, RedGrittyBrick <redgrittybr...@xxxxxxxxxxxxx>
wrote:

I am unable to make my JTable/JScrollPane so that it gains horizontal
crollbars when container width < JTable min width.

Any suggestions?

SSCCE
-----------------------------------8<---------------------------------
import java.awt.BorderLayout;

...

public class HorizScrollTable extends JPanel {

HorizScrollTable() {

column.setPreferredWidth(width);
column.setMinWidth(width); // [2]
}

JPanel p = new JPanel(new BorderLayout());
p.add(table, BorderLayout.CENTER); // [3]
// new ..
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());


Regards,

Bart

.



Relevant Pages

  • JViewPorts Client not refreshed
    ... JScrollPane scrollPane = new JScrollPane; ... I pull knob on the right and allways become(a picture). ...
    (comp.lang.java.programmer)
  • Re: JTable Resizing
    ... It's the JScrollPane you need to get to resize, ... You need to ensure that layout manager for the container in which you place ... University of Leicester, Leicester, LE1 7RH, UK ...
    (comp.lang.java.gui)
  • Re: Horizontally scrolling JTable
    ... crollbars when container width < JTable min width. ... JPanel p = new JPanel); ... JScrollPane scrollPane = new JScrollPane; ... Is it impossible to have horizontal scrolling AND headings? ...
    (comp.lang.java.gui)
  • problems refreshing GUI
    ... What I have is a Container with on it a JScrollPane to hold a JTable with ... data provided by a database. ... The only thing that almost worked is by placing it all on a JPanel and using ... It didn't show the old ones anymore but my JScrollPane isn't working ...
    (comp.lang.java.gui)
  • Re: JTable Resizing
    ... > Does anyone know how to get a JTable to resize to fill its parent ... In my case I have a JScrollPane which holds a JPanel ... When I resize the application, ...
    (comp.lang.java.gui)