Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to clip child from layout manager

22 views
Skip to first unread message

FredK

unread,
Apr 5, 2013, 1:03:34 PM4/5/13
to
I have a custom layout manager.
If the Swing container that uses this manager is too small to layout
the children at preferred sizes, I want to clip one or more of them.

I do not want to set the child's size smaller than its desired size.

For example, suppose the child's desired width is 100. I want to
place it at x=250 with width = 100, but I want to clip part of it so
that the first 40 pixels are clipped. The end result will be
that its position is [250,350] but only the child's [60,100] part
is visible ( occupying the region [290,350] ).
--
Fred K

markspace

unread,
Apr 5, 2013, 7:28:07 PM4/5/13
to
I think this happens automatically. I think the clip region of the
Graphic2D is already set for you when your layout manager is called.
Just call setBounds() on each object you want to lay out. They'll be
drawn at that position, even if only partly visible. If not visible it
won't be drawn at all, the underlying graphics tool is smart like that.


Roedy Green

unread,
Apr 5, 2013, 11:51:23 PM4/5/13
to
On Fri, 5 Apr 2013 10:03:34 -0700 (PDT), FredK
<fred.l.kl...@gmail.com> wrote, quoted or indirectly quoted
someone who said :

>If the Swing container that uses this manager is too small to layout
>the children at preferred sizes, I want to clip one or more of them.

This is an idea I have wanted to implement for a long time. You might
use some of the ideas.

Let us say I had a Demographics panel to display name, international
address and international phone numbers.

Given lots of space it would display name, address, country, postal
phone numbers.

If you prune back the space, the labels for fields disappear.

If you prune the space back further, it would use smaller fonts, and
shorter fields (that scroll),

If you prune space back further , the second address line disappears,
then the first. All but the first phone would disappear.

If you expand the space, bit by bit the material reappears.

The component is aware of how much space it has and renders
appropriately.

It would look at the particular data and adjust the layout., so it
would not waste a lot of space with trailing spaces in fields.
--
Roedy Green Canadian Mind Products http://mindprod.com
Motors make noise, and that tells you about the feelings and attitudes
that went into it. Something was more important than sensory pleasure --
nobody would invent a chair or dish that smelled bad or that made horrible
noises -- why were motors invented noisy? How could they possibly be
considered complete or successful inventions with this glaring defect?
Unless, of course, the aggressive, hostile, assaultive sound actually served
to express some impulse of the owner.
~ Philip Slater (born: 1927 age: 85)
The Wayward Gate: Science and the Supernatural

FredK

unread,
Apr 8, 2013, 10:38:16 AM4/8/13
to
On Friday, April 5, 2013 4:28:07 PM UTC-7, markspace wrote:
> On 4/5/2013 10:03 AM, FredK wrote: > I have a custom layout manager. > If the Swing container that uses this manager is too small to layout > the children at preferred sizes, I want to clip one or more of them. > > I do not want to set the child's size smaller than its desired size. > > For example, suppose the child's desired width is 100. I want to > place it at x=250 with width = 100, but I want to clip part of it so > that the first 40 pixels are clipped. The end result will be > that its position is [250,350] but only the child's [60,100] part > is visible ( occupying the region [290,350] ). > I think this happens automatically. I think the clip region of the Graphic2D is already set for you when your layout manager is called. Just call setBounds() on each object you want to lay out. They'll be drawn at that position, even if only partly visible. If not visible it won't be drawn at all, the underlying graphics tool is smart like that.


No, this is not what I want do do here. Calling setBound() with a size
smaller than the preferred size causes the component to try to draw itself
in that smaller size, and will clip anything that won't fit.

Here's what I want it to do. Suppose the child's preferred size is
200x500. I want it to draw itself at that size, with its UL corner at,
say, (200,400), so its LR corner will be at (400,900).

But I want to see only that portion of it that is within my
rectangle [upperleft=(250,350), lowerright=(350,500)]. Thus it clips
itself around all edges - similar to what you see in the viewport
of a ScrollPane

markspace

unread,
Apr 8, 2013, 1:00:26 PM4/8/13
to
On 4/8/2013 7:38 AM, FredK wrote:
> But I want to see only that portion of it that is within my
> rectangle [upperleft=(250,350), lowerright=(350,500)]. Thus it clips
> itself around all edges - similar to what you see in the viewport
> of a ScrollPane
>

No idea. Maybe try to put the component in a container like a panel,
and adjust the panel size so the components are clipped. Generally
speaking, I think Swing tries to display components, not hide them.


Daniel Pitts

unread,
Apr 8, 2013, 5:08:09 PM4/8/13
to
On 4/5/13 10:03 AM, FredK wrote:
> I have a custom layout manager.
That always seems dangerous to me, but for now I'll take your word that
it is actually the solution you want.
Sounds a bit like you want a JViewport. Which I believe you actually can
use without a JScrollPane.
0 new messages