JScrollPane in GridBagLayout is either at minimum or contents' size
This is the problem:
I try to use components that are contained within JScrollPanes in a
design that uses GridBagLayout as a layout-manager. But if the
component's size exceeds the space allotted to it by the layout-
manager, it is displayed at minimum size. Otherwise, it is displayed
at full size without any scrollbars needed.
The code below replicates the problem. Note that setting the layout-
manager to BorderLayout, putting the JScrollPane in the centre and
label2 in the east solves the problem - but this isn't an option in
the original program.
So, I need a JScrollBar that occupies all the space allotted by the
layout-manager. In the example given below, that would be all the
space available in the frame after placing label2.
import java.awt.*;
import javax.swing.*;
public class ScrollPaneResize extends JFrame{
ScrollPaneResize(){
this.setSize(100, 100);
this.setDefaultCloseOperation(ScrollPaneResize.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
JLabel label = new JLabel("some incredibly long text is put here,
that makes the label bigger than the frame's size");
JLabel label2 = new JLabel("shorttext");
JPanel panel = new JPanel();
panel.add(label);
JScrollPane scrollpane = new JScrollPane(panel);
this.getContentPane().add(scrollpane, constraints);
constraints.gridx = 1;
this.getContentPane().add(label2, constraints);
this.setVisible(true);
}
public static void main(String[] args){
ScrollPaneResize spc = new ScrollPaneResize();
}
}