0

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

Community
  • 1
  • 1
sethu
  • 8,181
  • 7
  • 39
  • 65
  • Try adding something to the scrollpane that implements the `Scrollable` interface. This can effect that "viewable" size of the scrollpane – MadProgrammer Jan 17 '14 at 06:17
  • I am adding many textfields, a JList to the JPanel which is then added to the JScrollPane. Or are you saying I must make the Jpanel I am adding to the JScrollPane implement Scrollable? If I do, how would I implement the methods on Scrollable in the JPanel? – sethu Jan 17 '14 at 06:24
  • 1
    For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). – Andrew Thompson Jan 17 '14 at 06:34
  • there are two ways set(Xxx)Size for JFrame or setPreferredSize (for JPanel, for JList or JTable is possible to set this size correct way) for JScrollPane directly, there isn't another proper way – mKorbel Jan 17 '14 at 07:32
  • @sethu The reason I asked for the MCVE (besides the obvious) is that it is typically better to size containers according to their content. OTOH I note that your last visit was 5 hours after my comment. Since you seem to be ignoring me, I'll just down-vote (for ignoring me) vote to close (for lack of demonstrative code) and move on. – Andrew Thompson Jan 18 '14 at 00:46
  • Hey Andrew..not fair. I didn't have the time to respond back with an MVCE at that time. Will get around to it today. But I do have an answer below by peeskillet which I want to try to see if it works as well. For the record, I dont ignore anyone who is kind enough to respond to a problem I have.. promise! :) – sethu Jan 18 '14 at 06:29
  • Sethu some tips: 1) Add @MadProgrammer (or whoever - the `@` is important) to *notify* them of a new comment. 2) The world is not fair. Deal with it. – Andrew Thompson Jan 21 '14 at 01:54

2 Answers2

0

"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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Could you please check the image posted above. – sethu Jan 19 '14 at 15:55
  • ok.. I missed the frame.setExtendedState(JFrame.MAXIMIZED_BOTH); in my example. Why does this behave this way? Only if I maximise both would it respect the task bar? – sethu Jan 19 '14 at 15:57
0

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

Community
  • 1
  • 1
sethu
  • 8,181
  • 7
  • 39
  • 65