Sizing a JScrollPane in a JTabbedPane in a JFrame

a 夏天 提交于 2019-12-02 11:44:43

"Make the JFrame/JTabbedPane/JPanelInTab extend upto the height of the screen (taking into the taskbar of windows), if the tab content is going to get cut the scrollbars should appear. The width of the frame should fit exactly as much as the JTabbedPane."

For each JScrollPane that holds each tab panbel, that you want to add to the JTabbedPane, wrap them each in another JPanel with a BorderLayout. The BorderLayout will cause the JScrollPanes to stretch out the size of the container JTabbedPane, which ultimately is stretched to the size of the JFrame

    // set tabbed panels to BorderLayout and add scrolls
    panelTab1 = new JPanel(new BorderLayout());
    panelTab2 = new JPanel(new BorderLayout());
    panelTab1.add(panel1Scroll);
    panelTab2.add(panel2Scroll);

    // add panelTabs to tabbed pane
    tabbedPane = new JTabbedPane();
    tabbedPane.add(panelTab1, "Panel 1");
    tabbedPane.add(panelTab2, "Panel 2");

You can see that I've done what I've described in my my answer above.

Here is the running example

import java.awt.BorderLayout;
import javax.swing.*;

public class TabbedMaxSize {
    JTabbedPane tabbedPane;
    JPanel panelTab1;
    JPanel panelTab2;

    public TabbedMaxSize() {
        // add label to first bos
        Box box1 = Box.createVerticalBox();
        for (int i = 1; i <= 100; i++) {
            box1.add(new JLabel("This is Label #" + i));
        }

        // add labels to second box
        Box box2 = Box.createVerticalBox();
        for (int i = 1; i <= 100; i++) {
            box2.add(new JLabel("This is Label #" + i));
        }

        // add boxes to panels and to scroll panes
        JPanel boxPanel1 = new JPanel();
        JPanel boxPanel2 = new JPanel();
        boxPanel1.add(box1);
        boxPanel2.add(box2);
        JScrollPane panel1Scroll = new JScrollPane(boxPanel1);
        JScrollPane panel2Scroll = new JScrollPane(boxPanel2);

        // ser tabbed panels to BorderLayout and add scrolls
        panelTab1 = new JPanel(new BorderLayout());
        panelTab2 = new JPanel(new BorderLayout());
        panelTab1.add(panel1Scroll);
        panelTab2.add(panel2Scroll);

        // add panelTabs to tabbed pane
        tabbedPane = new JTabbedPane();
        tabbedPane.add(panelTab1, "Panel 1");
        tabbedPane.add(panelTab2, "Panel 2");


        JFrame frame = new JFrame();
        frame.add(tabbedPane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                TabbedMaxSize gui = new TabbedMaxSize();
            }
        });
    }
}

sethu

I had posted this question again since this one was voted to be closed. Over there I have marked the right answer. Please check the link below for the answer

Use the Bounds of the Screen minus Insets

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