Size issues with JTabbedPane and fixed height content

旧街凉风 提交于 2019-12-01 23:56:12

After reading more about the way the preferred size of the tabbed pane is calculated, I have been able to come up with the following solution for the WRAP_TAB_LAYOUT case.

The problem is that for the tabbed pane, the preferred height and width are coupled. The preferred height is calculated for the preferred width and not for the actual, current width. This is problematic if the layout manager of the parent respects the preferred height but not the preferred width.

The solution I came up with is to set up a listener that sets the preferred width of each tab content to its current width. This way, the tabbed pane calculates its preferred height correctly and can be laid out e.g. with a BorderLayout.

tabs.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        JTabbedPane tabbedPane = (JTabbedPane) e.getComponent();
        int tabCount = tabbedPane.getTabCount();
        for (int i = 0; i < tabCount; i++) {
            Component c = tabbedPane.getComponentAt(i);
            c.setPreferredSize(new Dimension(c.getSize().width, c.getPreferredSize().height));
        }
    }
});

I haven’t been able to find a satisfactory solution to the SCROLL_TAB_LAYOUT case yet, so if anyone has an idea it would be appreciated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!