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()));
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
For each
JScrollPane
that holds each tab panbel, that you want to add to theJTabbedPane
, wrap them each in anotherJPanel
with aBorderLayout
. TheBorderLayout
will cause theJScrollPane
s to stretch out the size of the containerJTabbedPane
, which ultimately is stretched to the size of theJFrame
You can see that I've done what I've described in my my answer above.
Here is the running example