Re: JScrollPane in GridBagLayout is either at minimum or contents'
size
Icarus wrote:
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();
}
}
What is the preferredSize of your JScrollPane? It is the size of the
underlying JLabel, which on my computer is something like 500 pixels
wide. There are a lot of ways to solve your problem but one would be to
allow your component to fill the available space in your
GridBagLayout. It needs to be big enough to display it's scroll bars
and the data. Below I have modified your program to do just that. It
is not however an optimum solution. Better would be to detect the
preferred height and to set that on the scroll pane and only allow it to
only fill vertically.
Usually one sets a preferred size on a JScrollPane or allows it to fill
an available area. Constraining it in a GridBagLayout adds considerable
complexity to your layout.
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);
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = constraints.weighty = 1.0;
this.getContentPane().add(scrollpane, constraints);
constraints.weightx = constraints.weighty = 0.0;
constraints.gridx = 1;
this.getContentPane().add(label2, constraints);
this.setVisible(true);
}
public static void main(String[] args){
ScrollPaneResize spc = new ScrollPaneResize();
}
}
Thank you for posting a code that would in fact compile without too much
modification. One note however, please don't use tabs, use spaces for
indentation instead. It makes it considerably more easy to modify and
to repost.
--
Knute Johnson
email s/nospam/knute2008/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access