How to make JTable fill all the free space?

I want to create a panel with a table that fills all the available space.

I do this using the following code:

public class MyFrame extends JFrame { public EconomyFrame() throws HeadlessException { super("..."); final JTabbedPane tabbedPane = new JTabbedPane(); add(tabbedPane); final JPanel companiesPanel = new JPanel(); final CompaniesTableModel companiesModel = new CompaniesTableModel (ApplicationStateSingleton.INSTANCE.getPersistence().getCompanies()); final JTable companiesTable = new JTable(companiesModel); ApplicationStateSingleton.INSTANCE.getEventBus().subscribeToPropertyChanges(companiesModel); companiesPanel.add(new JScrollPane(companiesTable)); tabbedPane.addTab("Companies", companiesPanel); } } 

But this does not work, because when I change the frame size, the table fills only part of the available space (see screenshots below).

How can I fix it (make the table fill all the available space)?

Screen shot 1

Screenshothot 2

0
source share
2 answers

Use the layout manager, which allows JTable to occupy the entire available area, rather than the default FlowLayout used by JPanel , which uses only its preferred component sizes

 JPanel companiesPanel = new JPanel(new GridLayout()); 
+6
source

There are two problems:

1) First you need to use the appropriate layout manager to make sure scrollpane can resize to fill the available area. Normally, people would add scrollpane to the CENTER of BorderLayout.

2), you must let the table fill the available space in the scrollpane view. For this you use:

 table.setFillsViewportHeight(true); 
+5
source

Source: https://habr.com/ru/post/1394957/


All Articles