JScrollPane in GridBagLayout is either at minimum or contents' size


I

Icarus

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

Advertisements

K

Knute Johnson

Icarus said:
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.
 
I

Icarus

That does solve the problem in the simple example, and helps in my
more complex program as well. The problem is now, that the scrollbars
are now initialized at almost minimum height. I'll try to find a
solution on my own first, and if not, will try to construct some
sample code. But as for now, thank you for your help and the quick
replies.
 
I

Icarus

Alright, solved the problem as my design allowed to set the minimum
height of the components without interfering with the layout-manager.
So, consider this done.

As a side note: I am using Eclipse, hence the tabs. I will keep that
in mind for any postings in the future, though.


And thanks again.
 
A

Andrew Thompson

On Jul 2, 5:33 am, Knute Johnson <[email protected]>
wrote:
...
Thank you for posting a code that would in fact compile without too much
modification.  

Note to the OP:
Most people prefer an SSCCE*. A code sample
that compiles with *no* modification. You
were lucky to catch the attention of one of
the 'GUI gurus' with just a code snippet.

* said:
...One note however, please don't use tabs, use spaces for
indentation instead.  

Check out the 'Text Width Checker'** which is
intended to help with just that problem. It
even includes a button to 'replace tabs' with
a number of spaces.

** <http://pscode.org/twc/>
 
A

Andrew Thompson

Alright, solved the problem as my design allowed to set the minimum
height of the components without interfering with the layout-manager.
So, consider this done.

As a side note: I am using Eclipse, hence the tabs.

I don't use Eclipse, but are you seriously
suggesting that Eclipse cannot be..
a) Configured to use spaces as the default, or..
b) Does not provide a global 'find/replace' function?

If the answer to either question is 'no', then
perhaps you should consider using an IDE that
does not grab the short and curly hairs of your
genitals and yank them.
 
Ad

Advertisements

A

Andrew Thompson

How is Eclipse to blame?  You control Eclipse, which I happen to know is
capable of expanding TABs to spaces even if you couldn't do it yourself in the
editor manually, which you could.

It is a poor workman who blames his tools.

Mine was funnier*, but (concedes) your reply is
better balanced and more informative.

* According to the 'jury that is me'.
 
R

RedGrittyBrick

Icarus said:
As a side note: I am using Eclipse, hence the tabs. I will keep that
in mind for any postings in the future, though.

Project, Properties, Java Code Style, Formatter,
Change "Profile" from "Eclipse" to "Java Conventions".

or (for example)
Edit the profile and change Indentation, General Settings,
set Tab Policy to "Spaces only" or "Mixed"
untick [] "use tabs only for leading indentation".
Set indentation size to 4 (or 2)
 
Ad

Advertisements

P

Patricia Shanahan

Icarus said:
Alright, solved the problem as my design allowed to set the minimum
height of the components without interfering with the layout-manager.
So, consider this done.

As a side note: I am using Eclipse, hence the tabs. I will keep that
in mind for any postings in the future, though.

In Eclipse, the formatter options include "Tab policy". I use "Spaces
only" and set "Indentation size" to 2 for material I intend to post on
Usenet.

Patricia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top