Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2008

Tip: Looking for answers? Try searching our database.

JScrollPane content sizing

Thread view: 
MRe - 22 May 2008 09:35 GMT
Hi,

 I'm new to Java GUI programming, and it's been a constant struggle
to write a GUI that looks like it was on purpose. Well, JScrollPane
has been giving me more trouble  than it's worth, so as a last resort,
maybe someone could just tell me how to fix it?..

 What I want to do is have a JScrollPane containing a JPanel. The
JPanel should automatically size to the width of the JScrollPane's
viewport, and to the minimum height of the contents of the JPanel. The
idea being that the JPanel's controls spring to fit the width of the
JScrollPane, (never showing a horizontal scroll bar), but be compact
vertically, showing a vertical scrollbar when the JScrollPane is too
small.

 Hope that makes some sense; any help greatly appreciated

Thank you,
Kind regards,
Eliott
John - 22 May 2008 14:57 GMT
> Hi,
>
[quoted text clipped - 16 lines]
> Kind regards,
> Eliott

I've never tried to do quite what you are attempting, so I don't know
how helpful this will be.  If you post some example code, it may make
it easier for people to give you help.

The JOptionPane usually sizes itself to fit what it contains. As an
experiment, create a JTextArea using new JTextArea(), and add it to a
JScrollPane with new JScrollPane(the JTextArea).  When you layout the
JScrollPane and show it on the screen, you won't be able to see the
scroll pane because it is 1 by 1 pixel in size.  Now go back and
create the JTextArea with new JTextArea(80, 80).  Now you can see the
scroll pane because the text area has a size that the viewport can use
to set its size.

I'm not sure what will happen if you remove and then add different
objects to the scroll pane.  You might try using a card layout, and
having a JTextArea or something else with a well defined size as the
first object in the layout.  You could then add and remove the other
members to the card layout and only show the object you want to show.
I would guess that you will have to play around with setting
preferred, or perhaps minimum and maximum sizes, to get the exact
effect you want.

Good luck,

John
Andrew Thompson - 22 May 2008 15:19 GMT
...
> ...If you post some example code, it may make
> it easier for people to give you help.

In these cases, I usually recommend a specific
form of 'some example code' - an SSCCE*.

* <http://sscce.org>

--
Andrew T.
PhySci.org
MRe - 23 May 2008 10:51 GMT
Hi,

 Thanks for the responses.

 I can't really give example code, as I don't know where to begin..
What I've done so far has been mostly reading tutorials and API
documents - any code I've written has done nothing like I want it
(it's all new to me), so I wouldn't know what's a demonstrationable
version of what I want (it'd probably just make my sketchy problem
description more confusing).  I'll continue to hack through; if I
still can't figure it out maybe I'll draw a picture to explain..

Thanks again,
Kind regards,
Eliott
John - 23 May 2008 12:17 GMT
The Scrollable interface is what you are looking for. If you override
the "getScrollableTracksViewportWidth()" method for your JPanel, and
make it return true. The consequence is that the JPanel will have the
size of the JViewport width.

I have a question myself, which is related to the thread:

I have a JList in a JViewport, in a JScrollPane.

I want the opposite: the JScrollPane width should be the preferred width
of the JList, but i don't manage to do it. The JViewport has the
preferred size of the JList, but it's actual wdith is smaller.

Help apreciated :)

MRe a écrit :
> Hi,
>
[quoted text clipped - 11 lines]
> Kind regards,
> Eliott
Daniele Futtorovic - 24 May 2008 03:03 GMT
> The Scrollable interface is what you are looking for. If you override
> the "getScrollableTracksViewportWidth()" method for your JPanel, and
> make it return true. The consequence is that the JPanel will have the
> size of the JViewport width.

That is indeed the answer the OP needed, AFAICT.

> I have a question myself, which is related to the thread:
>
[quoted text clipped - 3 lines]
> of the JList, but i don't manage to do it. The JViewport has the
> preferred size of the JList, but it's actual wdith is smaller.

Then set the minimum size.

You may set this up dynamically by subclassing the JViewPort's
getPreferredSize() and getMinimumSize() (and maybe getMaximumSize()) and
modifying the returned java.awt.Dimension Object to have its width field
match the desired value.
This might not be the most elegant solution, but it should work and has
the advantage being appliable to all (proper) LayoutManagers.

PS: please do not top-post.

Signature

DF.
to reply privately, change the top-level domain
in the FROM address from "invalid" to "net"

Knute Johnson - 24 May 2008 00:02 GMT
> Hi,
>
[quoted text clipped - 16 lines]
> Kind regards,
> Eliott

Layouts are one of the most difficult things to learn in Java.  All
layout managers respond to different conditions, some
minimum/maximum/preferred size, some the size of the container and some
both.

Remember that if you add a component to a ScrollPane constructor that
component is the ViewPort and will follow the rules of the
ScrollPaneLayout.  It appears to be very similar the the center of a
BorderLayout in action.

I wrote a little test program that shows the size of a JPanel in a
JScrollPane.  The JScrollPane only shows vertical scroll bars, never
horizontal.  The JScrollPaneLayout will allow the JPanel to expand
larger than its preferred or maximum size (just as BorderLayout would).
It can never be smaller than the minimum width but if the JScrollPane
has been resized smaller than that minimum size some of the JPanel is
not visible.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test5 extends JPanel {
    public test5() {
    }

    public void paintComponent(Graphics g) {
        g.setColor(getBackground());
        g.fillRect(0,0,getWidth(),getHeight());
        g.setColor(Color.WHITE);
        g.drawString("Width:   " + Integer.toString(getWidth()),10,20);
        g.drawString("Height:  " + Integer.toString(getHeight()),10,40);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                test5 p = new test5();
                p.setBackground(Color.RED);
                p.setMinimumSize(new Dimension(100,75));
                p.setPreferredSize(new Dimension(200,150));
                p.setMaximumSize(new Dimension(400,300));

                JScrollPane sp = new JScrollPane(p,
                 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

                f.add(sp);

                f.pack();
                f.setVisible(true);
            }
        });
    }
}

If you want to get more specific about how you want your layout to look
and what components are on your JPanel, we can probably get you closer
to your goal.

Signature

Knute Johnson
email s/knute/nospam/

MRe - 26 May 2008 13:24 GMT
Hi,

<snip MRe>
> >   What I want to do is have a JScrollPane containing a JPanel. The
> > JPanel should automatically size to the width of the JScrollPane's
> > viewport, and to the minimum height of the contents of the JPanel.
</snip>
<snip Knute Johnson>
> Remember that if you add a component to a ScrollPane constructor that
> component is the ViewPort and will follow the rules of the
[quoted text clipped - 8 lines]
> has been resized smaller than that minimum size some of the JPanel is
> not visible.
</snip>

 This has fixed it for me, thank you very much for the sample code;
it's all seems quite easy once you know the secrets :)

Thanks again,
Kind regards,
Eliott

<snip Knute Johnson>
> import java.awt.*;
> import java.awt.event.*;
[quoted text clipped - 37 lines]
>
> }
</snip>
Knute Johnson - 26 May 2008 18:02 GMT
> Hi,
>
[quoted text clipped - 24 lines]
> Kind regards,
> Eliott

You are very welcome.  When you have multiple components sometimes it is
complicated to get just the right sizing, especially if you change the
size of the top most container.

Signature

Knute Johnson
email s/knute/nospam/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.