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 / September 2004

Tip: Looking for answers? Try searching our database.

JlayeredPane and own layoutmanager2 problem

Thread view: 
tompie - 23 Sep 2004 12:54 GMT
Hi,
I've been developing an application, where in one part of it i need to
drag Jbutton(s) to JPanel. First i used absolute values for JPanels and
JButtons (to try out how i can make JLayeredPane respond to dragging). I
used listeners to promote button to draglayer and back to normal layer
again after succesful dragging. Now i wanted to make it a better and
more scalable and made my own Layoutmanager2.
This caused a problem, it seems that the setLayer(comp,layer) isn't
working anymore. The Jbutton i try to promote still seems to be in
normal layer and thus it is dragged behind JPanels. I traced the
setLayer -call to my own layoutmanager to addLayoutComponent(Component
comp, Object parameters), which in my case only picks up new items to
layout. Should the setLayer -call be handled in my addLayoutComponent
-part anyway, or call something else or what? I am a bit confused why
the setLayer even calls addLayoutComponent.
If you have any pointers to where to look net, thanks.
T:Tomi
Andrew Thompson - 23 Sep 2004 13:17 GMT
> I've been developing an application, where in one part of it i need to
> drag Jbutton(s)

Please try to be accurate with Upper Case in Java
core classes.  After all, Java is case sensitive.

>..to JPanel. First i used absolute values for JPanels and
> JButtons

Setting absolute values is usually not a good idea.
It is good that you..

>..(to try out how i can make JLayeredPane respond to dragging). I
> used listeners to promote button to draglayer and back to normal layer
> again after succesful dragging. Now i wanted to make it a better and
> more scalable and made my own Layoutmanager2.

..make your own LayoutManager, but ..what is special
about this?  I wrote a ScrollLayout, that ..guess
what it does with components?   ;-)

But your LayoutManager (and I hope you stuck by
'L' & 'M' on you layout name) is called '2'..

That is not a very descriptive or useful name,
what is the best one or two words that describe
your layout?  How is it different to other layouts?

> This caused a problem, it seems that the setLayer(comp,layer) isn't
> working anymore.

And at this point, I'm thinking, this is a wonderful
description of the problems you are seeing with your
code, ..but let's see the code.

Here are some tips for producing a short, self contained
example to help others spot the problem that you cannot
<http://www.physci.org/codes/sscce.jsp>

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Babu Kalakrishnan - 23 Sep 2004 15:45 GMT
> ..make your own LayoutManager, but ..what is special
> about this?  I wrote a ScrollLayout, that ..guess
[quoted text clipped - 6 lines]
> what is the best one or two words that describe
> your layout?  How is it different to other layouts?

Shouldn't you be asking this question to Sun ?

From what I could make out, the OP was referring to his own
*implementation* of the java.awt.LayoutManager2 interface (forgetting
the lower case "m" for a moment).

And by the way :

JLayoutPane is one of the places where you would normally recommend
going in for a null LayoutManager because the LayoutManager or
LayoutManager2 interfaces are in my opinion quite inadequate in
its ability to define a 3 dimensional layout with overlapped components.
(Even JDesktopPane the only descendant of JLayeredPane in the library
does us a null LayoutManager)

BK
Andrew Thompson - 23 Sep 2004 16:09 GMT
(A.T. on LayoutManager2)
>> That is not a very descriptive or useful name,
>> what is the best one or two words that describe
>> your layout?  How is it different to other layouts?
..
> Shouldn't you be asking this question to Sun ?

(Squints, checks JavaDocs)
LOL!  That's a very good point Babu!  

I had never directly used it, and had
not noticed it there in the API!

My apologies to the OP.

..And to Sun, if you happen to be listenning.
Where *were* your head's at, when you named
that class?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.lensescapes.com/  Images that escape the mundane

Babu Kalakrishnan - 23 Sep 2004 15:29 GMT
> I've been developing an application, where in one part of it i need to
> drag Jbutton(s) to JPanel. First i used absolute values for JPanels and
[quoted text clipped - 10 lines]
> -part anyway, or call something else or what? I am a bit confused why
> the setLayer even calls addLayoutComponent.

The "layered" behaviour is managed by JLayoutPane by reordering the
components in its children list (the one returned by getComponents()).
So when you call setLayer, it might remove the component from itself and
add it again at a different index, and this will result in a remove and
add calls to the LayoutManager.

However, I don't see why installing a custom LayoutManager2
implementation should affect the Z order in any manner. The add / remove
calls to the LayoutManager is done solely for the purpose of
notification for it to be able to determine the placement of the
component during validation.

Are you by any chance subclassing JLayoutPane and overriding any of its
methods (like add,remove etc) ?

BK
tompie - 27 Sep 2004 08:58 GMT
>> I've been developing an application, where in one part of it i need to
>> drag Jbutton(s) to JPanel. First i used absolute values for JPanels
[quoted text clipped - 27 lines]
>
> BK

Hi,
Trying to be more pedantic now about the upper/lowercase. And being more
precise about the problem. No i didn't subclass JLayoutPane, not even
JLayeredPane.
What i'm trying to accomplish:
---------------------------
|    ---    ---    ---    |
|    |  |   |  |   |  |   |
[quoted text clipped - 3 lines]
| --  --  --  --  --  --  |
| ||  ||  ||  ||  ||  ||  |
---------------------------
I need a few JPanels, (2-4) in upper 80% of view and a few draggable
JButtons in lower 20%. Now i tried this with JLayeredPane and GridLayout
where JPanels were in their own 80% and JButtons were in theirs 20 %.
This also didn't work, the layout was ok, but the layers didn't work. So
i did it with null layout and absolute values and it worked ok. I tried
to do my own LayoutManager which does the same as GridLayout. Idea was
to make simple LayoutManager that AFAIK doesn't kill the setLayer but to
no avail. (I implemented the Java.awt.LayoutManager2)
I'll copy the addLayoutComponent which is called after setLayer:

  public void addLayoutComponent(Component comp, Object parameters)
//if Object is layoutObject
  { //add to vector, with parameters
    if (parameters != null)
    {
      layoutObject my_params = (layoutObject)parameters;
      int inner_container = my_params.getInnerContainer(); //to which
split i belong to
      int orientation = my_params.getOrientation(); //my place in vector
      LayoutVectorObject my_object;
      my_object = new LayoutVectorObject(my_params, comp);
      Vector my_vector = (Vector)vector_collection.get(inner_container);
      if (orientation > 0)
      {
        my_vector.add(orientation,my_object);
      }
      else
      {
        my_vector.add(my_object);
      }
    }
  }

After this the layoutContainer(Container parent) is called, it counts
and sets component to their places. No hassle there. The thing is as you
suspected this addLayoutComponent is called with null values, that's why
i use "if (parameters != null)" to handle them. Should it be handled
differently?

--quote--
So when you call setLayer, it might remove the component from itself and
add it again at a different index, and this will result in a remove and
add calls to the LayoutManager.
--qoute--

If this is the method of setLayer, should i just reorder the components
and "redraw" them myself? I really thought there was a more
sophisticated method for this. Also this might be the origin of the
problem, if setLayer just reorganises the components and redraws them,
and after that the LayoutManager redraws them again, in original order,
the setLayer is of no use. Well i'll check that now and report if that
was the case.
Thanks, Tomi
tompie - 28 Sep 2004 12:16 GMT
Hi all,
This problem seemed to solve itself... When i started to extend my
original JFrame which contained JLayeredPane all worked ok. There must
have been a scoping error on my part earlier(Well, i tried many
different solutions and did then other part of the program and got back
to this problem, rewrote the code and miracle had happened, it worked).
I'll propably also try if i can use GridLayout with this correctly. Let
you know then.
Thanks,
Tomi
Babu Kalakrishnan - 28 Sep 2004 17:07 GMT
>> Are you by any chance subclassing JLayoutPane and overriding any of
>> its methods (like add,remove etc) ?

> Trying to be more pedantic now about the upper/lowercase. And being more
> precise about the problem. No i didn't subclass JLayoutPane, not even
> JLayeredPane.

Sorry got LayoutManager and JLayeredPane mixed up  - I meant
JLayeredPane not JLayoutPane :-)

Glad that the problem fixed itself, but just one clarification :
> --quote--
> So when you call setLayer, it might remove the component from itself and
[quoted text clipped - 9 lines]
> the setLayer is of no use. Well i'll check that now and report if that
> was the case.

You could actually do the reordering yourself - it should achieve the
same effect. But the only thing is that the reordering must be done in
the "components" list of the container - not that of the LayoutManager,
unless you plan to override the paintChildren method and do the painting
in a specific order. The LayoutManager is not even notified when a
paint() event takes place, so its inernal ordering of components
wouldn't be relevant. (The LayoutManager is supposed to finish its job
during the validation process, and its only job is to assign the bounds
of each child component - it will not get called while a drag is
occurring unless you invoke a revalidate() before the repaint() call).

BK


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.