Dynamically change cell height in JTable using GridBagLayout

I ran into a problem when I have a JTable laid out using the GridBagLayout manager and the number of rows in the table is not fixed. I intend to fill the space assigned to the table by all its cells, however there is a large empty (and white) space between the last row of the table and the next component in the container.

As a potential solution, I am wondering if I can adjust the cell height to fill the space assigned to the table. So, for example, if there are three lines, then the height of the space should be divided into three lines. If there is only one row, then this row should occupy all the free space.

Any suggestion is welcome, and if there is a better way to achieve the desired effect, please enlighten me. Thank.

PS I use JTable inside JPanel instead of JScrollPane, if that matters.

Edit: So, I tried the following code, which of course adjusts the height of the lines depending on the number of lines present, but still leaves a blank space after the last line and before the next component. I wonder why?

// re-size the header and row height to fill the whole tPanel
int panelHeight = tPanel.getHeight();
int desiredRowHeight = panelHeight / (numOfRows + 1);
friendsInfo.getTableHeader().setPreferredSize(new Dimension(table.getColumnModel().getTotalColumnWidth(), desiredRowHeight));
table.setRowHeight(desiredRowHeight);
+3
source share
1 answer

Yes, you can. Just set the line height. You should use it here . Obviously, I assume that your panel has a "constant" size. Otherwise, the layout will resize it to the appropriate size, i.e. Table size.

, , .

EDIT1: , , GridBagLayout. , , ( ), . - , .

. ( ). , , / , , ( ).

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.JTableHeader;

public class TableRowResizeTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {   
                final JTable table = new JTable(3,3);
                final JTableHeader tHeader = table.getTableHeader();
                final JPanel tPanel = new JPanel(new GridBagLayout());
                tPanel.setBackground(Color.CYAN);
                ComponentListener cL = new ComponentAdapter() 
                {
                    @Override
                    public void componentResized(ComponentEvent e)
                    {
                        super.componentResized(e);
                        // re-size the header and row height to fill the whole tPanel
                        int panelHeight = tPanel.getHeight();
                        int numOfRows = table.getRowCount();
                        int desiredRowHeight = panelHeight / (numOfRows + 1);
                        int gap = panelHeight - desiredRowHeight * (numOfRows + 1);
                        tHeader.setPreferredSize(new Dimension(tHeader.getPreferredSize().width, 
                                desiredRowHeight+gap));
                        tHeader.revalidate();
                        tHeader.repaint();
                        if(desiredRowHeight <1)
                            desiredRowHeight = 1;
                        table.setRowHeight(desiredRowHeight);
                        table.revalidate();
                        table.repaint();
                        System.out.println("tPanel componentResized p.h="+tPanel.getHeight()
                            +"; desiredRowHeight="+desiredRowHeight+"; gap="+gap);
                    }                   
                };
                tPanel.addComponentListener(cL);
                GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.VERTICAL;
                c.gridx = 0;
                c.gridy = 0;
                c.weighty = 1.0;
                tPanel.add(tHeader, c);
                c.gridy = 1;
                c.weighty = 0.0;
                tPanel.add(table,c);
                JPanel contentPane = new JPanel(new BorderLayout());
                contentPane.setBackground(Color.RED);
                contentPane.add(tPanel);
                JFrame f = new JFrame();
                f.setContentPane(contentPane);
                f.setSize(800, 600);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                System.out.println("before f.setVisible p.h="+tPanel.getHeight());
                f.setVisible(true); 
                System.out.println("after f.setVisible p.h="+tPanel.getHeight());
            }
        });
    }
}
+2

All Articles