Sizing a JScrollPane in a JTabbedPane in a JFrame

2019-03-03 10:30发布

I understand from this post Don't Set sizes on a JComponent that you dont set preferred sizes on any component and instead let the layout manager do this for you.

My question is, I have a JPanel that I put into a JTabbedPane into a JFrame like this

JFrame frame=new JFrame(); 
JTabbedPane pane=new JTabbedPane();
pane.addTab("Tab 1",new JScrollPane(getJPanel1()));
pane.addTab("Tab 2",new JScrollPane(getJPanel2()));
frame.setContentPane(pane);

Now in this case the JTabbedPane will take the size of the maximum sized component you add to it. Because of this my JScrollPane does not show up at all. I need to set the preferred size of the JScrollPane, if I dont set it, the scroll bars will not appear and content is getting cut.

How do I use a layout manager to solve this. I want to specifically do this:

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.

EDIT

Here's an MVCE that shows what I am trying to do. I have included the changes as per the suggestion by peeskillet but there is no effect

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

public class ScrollPaneTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame=new JFrame();
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

                JTabbedPane tabbedPane=new JTabbedPane();

                tabbedPane.addTab("Tab 1", wrapWithBorderLayoutPanel(getPanel()));
                tabbedPane.addTab("Tab 2", wrapWithBorderLayoutPanel(getPanel()));
//
//                tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
//                tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

                frame.setContentPane(tabbedPane);
                frame.pack();
                frame.setVisible(true);
            }

            private JPanel wrapWithBorderLayoutPanel(JPanel panel) {
                JPanel borderLayoutPanel=new JPanel(new BorderLayout());
                borderLayoutPanel.add(new JScrollPane(panel), BorderLayout.CENTER);
                return borderLayoutPanel;
            }

            private JPanel getPanel() {
                JPanel panel=new JPanel();
                Box box = Box.createVerticalBox();
                for (int i = 1; i <= 100; i++) {
                    box.add(new JLabel("This is Label #" + i));
                }
                panel.add(box);
                return panel;
            }
        });
    }

}

Once I do that the below is the output I get. The frame does not end at the taskbar. It stretches behind it. So the last label is hidden behind the taskbar. I would like the frame to end before the taskbar begins.

PS: There is no change if I wrap the panel with a BorderLayout panel or add the scroll pane directly to the tabbed pane. Both results in the same thing. You can test the same by commenting out lines

           //tabbedPane.addTab("Tab 1", wrapWithBorderLayoutPanel(getPanel()));
            //tabbedPane.addTab("Tab 2", wrapWithBorderLayoutPanel(getPanel()));

            tabbedPane.addTab("Tab 1", new JScrollPane(getPanel()));
            tabbedPane.addTab("Tab 2", new JScrollPane(getPanel()));

enter image description here

2条回答
我命由我不由天
2楼-- · 2019-03-03 10:33

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

查看更多
forever°为你锁心
3楼-- · 2019-03-03 10:40

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

enter image description here

查看更多
登录 后发表回答