4

I have a JTable with autoResizeMode set to AUTO_RESIZE_LAST_COLUMN. I have added it to a panel by creating a JScrollPane with the JTable as its child widget, and then adding the JScrollPane to the panel.

I would like to set the size of the JScrollPane viewport to that of the parent JPanel, and have the JTable resize its last column dynamically.

Martin DeMello
  • 11,876
  • 7
  • 49
  • 64
  • 1
    What did you try sofar ? what exaclty doesnt work ? SHow a simple example that actually runs and describe what isent working. – Peter Feb 06 '12 at 06:34
  • in Swing/AWT, the concept of "set size to .." is incorrect - locating/sizing is the job of the LayoutManager, which all behave differently. Developers choose one which produces the result they want to achieve :-) – kleopatra Feb 06 '12 at 09:13

3 Answers3

4

I think if you make the JPanel use GridLayout(1,1) and add the JScrollPane to it then you will get the desired result.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19
4

JPanel have got implemented FlowLayout by defaut, you can place JScrollPane to the BorderLayout.CENTER

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    Downvoted because this answer doesn't actually show the code for what to do, and it's unclear (at least to me) exactly what is being suggested. – JakeRobb Aug 17 '16 at 14:17
0

The first answer on this link How to make a JTable fill entire available space? worked perfectly for me.

I did the following

myPanel = new JPanel(new GridLayout());
myJtable = new JTable(MyTableModel);
myPanel.add(new JScrollPane(myJtable));
Community
  • 1
  • 1
Joseph
  • 789
  • 1
  • 9
  • 23