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

25
задан Leon 24 May 2011 в 02:03
поделиться

1 ответ

Я обнаружил, что все, что нужно, это включить

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

, а затем, когда требуемая ширина и высота области просмотра были рассчитаны, включить

  frame.getContentPane().add(new JScrollPane(table))
  table.setPreferredScrollableViewportSize(new Dimension(width,height));
2
ответ дан 28 November 2019 в 20:51
поделиться
Другие вопросы по тегам:

Похожие вопросы: