JScrollPane in GridBagLayout is either at minimum or contents' size

Discussion in 'Java' started by Icarus, Jul 1, 2008.

  1. Icarus

    Icarus Guest

    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();
    }
    }
    Icarus, Jul 1, 2008
    #1
    1. Advertising

  2. 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
    Knute Johnson, Jul 1, 2008
    #2
    1. Advertising

  3. Icarus

    Icarus Guest

    Re: JScrollPane in GridBagLayout is either at minimum or contents'size

    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.
    Icarus, Jul 2, 2008
    #3
  4. Icarus

    Icarus Guest

    Re: JScrollPane in GridBagLayout is either at minimum or contents'size

    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.
    Icarus, Jul 2, 2008
    #4
  5. Re: JScrollPane in GridBagLayout is either at minimum or contents'size

    On Jul 2, 5:33 am, Knute Johnson <>
    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.

    * <http://pscode.org/sscce.html>

    >...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/>

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jul 2, 2008
    #5
  6. Re: JScrollPane in GridBagLayout is either at minimum or contents'size

    On Jul 2, 8:41 pm, Icarus <> wrote:
    > 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.

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jul 2, 2008
    #6
  7. Re: JScrollPane in GridBagLayout is either at minimum or contents'size

    On Jul 2, 9:28 pm, Lew <> wrote:
    > Icarus wrote:
    > > As a side note: I am using Eclipse, hence the tabs. I will keep that
    > > in mind for any postings in the future, though.

    >
    > 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'.

    --
    Andrew Thompson
    http://pscode.org/
    Andrew Thompson, Jul 2, 2008
    #7
  8. Re: JScrollPane in GridBagLayout is either at minimum or contents'size

    Icarus wrote:
    > 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)

    --
    RGB
    RedGrittyBrick, Jul 2, 2008
    #8
  9. Eclipse formatter options. Was: Re: JScrollPane in GridBagLayoutis either at minimum or contents' size

    Icarus wrote:
    > 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
    Patricia Shanahan, Jul 2, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. electric sheep

    Enforce minimum window size

    electric sheep, May 19, 2004, in forum: Java
    Replies:
    5
    Views:
    12,508
    Andrew Thompson
    May 21, 2004
  2. Brandon McCombs

    minimum size for a jframe

    Brandon McCombs, Mar 27, 2006, in forum: Java
    Replies:
    2
    Views:
    3,548
    Alex Hunsley
    Mar 28, 2006
  3. luc h
    Replies:
    3
    Views:
    516
    Victor Bazarov
    Jan 9, 2004
  4. Allan Valeriano

    Set size of JTable inside a JScrollPane

    Allan Valeriano, Jun 15, 2007, in forum: Java
    Replies:
    1
    Views:
    13,158
    Andrew Thompson
    Jun 16, 2007
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,518
    Michael Jung
    May 25, 2008
Loading...

Share This Page