Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JTable both AutoResize and horizontall scrollable?

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

like image 865
Leon Avatar asked May 24 '11 01:05

Leon


People also ask

How do I make my JTable scroll horizontal?

To create a scrollable JTable component we have to use a JScrollPane as the container of the JTable . Besides, that we also need to set the table auto resize mode to JTable. AUTO_RESIZE_OFF so that a horizontal scroll bar displayed by the scroll pane when needed.

What is a JScrollPane?

A JScrollPane provides a scrollable view of a component. When screen real estate is limited, use a scroll pane to display a component that is large or one whose size can change dynamically. Other containers used to save screen space include split panes and tabbed panes.


2 Answers

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.

like image 112
camickr Avatar answered Sep 30 '22 08:09

camickr


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);         }     } }); 
like image 43
gb96 Avatar answered Sep 30 '22 07:09

gb96