简体   繁体   English

使用GridBagLayout动态更改JTable中的单元格高度

[英]Dynamically change the cell height in JTable with a GridBagLayout

I run into a problem where I have a JTable laid out using GridBagLayout manager and the number of rows in the table is not fixed. 我遇到了一个问题,我使用GridBagLayout管理器布局JTable并且表中的行数不固定。 I intend to fill the space assigned to the table with all its cells, however there is a big blank (and white) space between the last row of the table and the next component in the container. 我打算用它的所有单元格填充分配给表的空间,但是在表的最后一行和容器中的下一个组件之间有一个大的空白(和白色)空间。

As a potential solution, I wonder if I can adjust the height of the cells to fill the space assigned to the table. 作为一个潜在的解决方案,我想知道我是否可以调整单元格的高度来填充分配给表格的空间。 So for example, if there are three rows, then the height of the space should be equally divided into three rows. 因此,例如,如果有三行,则空间的高度应平均分为三行。 If there is only one row, then this row should take up the whole space available. 如果只有一行,则该行应占用整个可用空间。

Any suggestion are welcome, and if there is a better way to achieve the desired effect, please enlighten me. 欢迎任何建议,如果有更好的方法达到预期的效果,请赐教。 Thanks. 谢谢。

PS I am using JTable within a JPanel instead of a JScrollPane, if that makes any difference. PS我在JPanel中使用JTable而不是JScrollPane,如果这有任何区别的话。

Edit: So I've tried the following code, which certainly adjusts height of the rows depending on the number of the rows present, but it still leaves a blank white space after the last row and before the next component. 编辑:所以我尝试了下面的代码,它肯定根据存在的行数调整行的高度,但它仍然在最后一行之后和下一个组件之前留下一个空白空格。 Wondering 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);

Yes you can. 是的你可以。 Just set the row heights. 只需设置行高。 You would have to use something like here . 你必须使用像这里的东西。 Obviously I assume here that your panel has a 'constant' size. 显然我在这里假设您的面板具有“恒定”尺寸。 Otherwise the layout will make sure to resize it to appropriate size, ie table size. 否则布局将确保将其调整为适当的大小,即表大小。

If yes, then you can use the panel height for your calculations of the height each row should take. 如果是,那么您可以使用面板高度来计算每行应该采用的高度。

EDIT1: Below is an example showing how it might be used to do so using GridBagLayout. EDIT1:下面是一个示例,说明如何使用GridBagLayout来实现它。 I did all to make it look as best as possible though still it has a strange behaviour (for a short time it gets minimal size) when you making the frame smaller. 我做了所有让它看起来尽可能最好尽管它仍然有一个奇怪的行为(在短时间内它获得最小的尺寸)当你使框架更小。 But then again it might be default behaviour for the layout which I am not aware of. 但是,这可能是布局的默认行为,我不知道。

NOTE: I am not an expert in using this layout manager (personally I hate it). 注意:我不是使用此布局管理器的专家(我个人讨厌它)。 Thus if there are some parameters which should/shouldn't be set please do let me know (and feel free to edit the answer). 因此,如果有一些参数应该/不应该设置,请告诉我(并随时编辑答案)。

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());
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM