How to make JTable both AutoResize and horizontall scrollable?
Asked Answered
S

4

29

I am putting a JTable into a JScrollPane

But When I set JTable Auto Resizeable, then it won't have horizontal scroll bar.

if I set AUTO_RESIZE_OFF, then the Jtable won't fill the width of its container when the column width is not big enough.

So how can I do this:

  1. when the table is not wide enough, expand to fill its container width
  2. when the table is wide enough, make it scrollable.

Thanks

Stateless answered 24/5, 2011 at 1:40 Comment(0)
O
43

You need to customize the behaviour of the Scrollable interface.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableHorizontal extends JFrame
{
    public TableHorizontal()
    {
        final JTable table = new JTable(10, 5)
        {
            public boolean getScrollableTracksViewportWidth()
            {
                return getPreferredSize().width < getParent().getWidth();
            }
        };
        table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
        final JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );
    }

    public static void main(String[] args)
    {
        TableHorizontal frame = new TableHorizontal();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setSize(400, 300);
        frame.setVisible(true);
    }
}

The above code basically sizes the component at its preferred size or the viewport size, whichever is greater.

Opinionative answered 24/5, 2011 at 1:48 Comment(3)
excelent knowledge +1, for setSize -1Alfreda
ahhh ... Row, your trick - should have guessed so much :-) But it's uncomplete, hampers resizing of columns, see https://mcmap.net/q/501796/-jtable-with-autoresize-horizontal-scrolling-and-shrinkable-first-column/203657Nagging
@kleopatra, I learn as I answer more questions (and see other answers). See #15235191 for an updated approach.Opinionative
R
11

If for some reason customising JTable is not an option (e.g. it might be created in third-party code), you can achieve the same result by setting it to toggle between two different JTable AUTO_RESIZE modes whenever the containing viewport is resized, e.g.:

jTable.getParent().addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(final ComponentEvent e) {
        if (jTable.getPreferredSize().width < jTable.getParent().getWidth()) {
            jTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        } else {
            jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        }
    }
});
Retral answered 27/6, 2012 at 22:11 Comment(1)
Worked like a charm! Thank you for the simple solution.Unrealizable
I
2

I found that all that is needed is to include

  table = new JTable(model);
  // this enables horizontal scroll bar
  table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );    

and then when the required viewport width and height have been calculated, include

  frame.getContentPane().add(new JScrollPane(table))
  table.setPreferredScrollableViewportSize(new Dimension(width,height));
Incompetence answered 20/9, 2013 at 11:59 Comment(0)
H
0

If you set the Layout of its container to BorderLayout with a BorderLayout.CENTER layout constraint, then the JTable will auto resize to fit its container.

If you want to make a component scrollable, you can wrap the JTable with a JScrollPane.

setLayout(new BorderLayout());
add(new JScrollPane(new JTable()), BorderLayout.CENTER);
Hadfield answered 24/5, 2011 at 1:46 Comment(1)
This only auto resizes for the width not height. How can I make it auto resize the height?Licht

© 2022 - 2024 — McMap. All rights reserved.