🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #13327

Re: Changing Z-order with swing components

#13327
From: John P. McGrath
Date: Wed, 12 Aug 1998 00:00
36 lines
2266 bytes
zav <zav@gic-net.com> wrote:

> I have noticed that there is no bringToFront() or sendToBack() methods
> for JComponents in order to change the Z-order of the GUI.  I have some
> JPanels which are children on a parent JPanel and when I click and drag
> one of them I want it to appear on top of all the others...how can I
> bring a panel to the front or send the others back in the Z-order...I
> guess the JVM has it's own interpretation of Z-order which may be
> different to the host operating system...I would very much appreciate
> any help out there...Thanks and have a good day!

The way to do this is to remove() the component from the container and then add() it again.  If you do not remove it first, it will be automatically removed first, but I would recommend doing the remove.  Otherwise it makes the interpretation of the add() index argument a little confusing.

When you add the component, if you do not specify an index, it goes at the "end" (as the Sun docs say), which means in the back.  (I have always thought this was strange -- the default should be on top).  To bring it to the front, you specify the index equal to the number of components in the window.

This may seem strange, but it really is not much of a problem.  It was a problem with heavyweight AWT components, since this resulted in the peer being deleted and then re-created.  But with Swing, there is not much that would be saved with a moveToFront() or moveToBack() method.  However, it would be a lot nicer if Sun would add those methods -- it would make the code a _lot_ more clear.

-- 
John P. McGrath
<mcgrath@enter.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <01bdc5c5$c6cb18b0$1a47aacc@ferd>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <35C96DEF.5737511D@gic-net.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,f15b637c03713beb
X-Google-Attributes: gid111d45,public
From: "John P. McGrath" <mcgrath@enter.net>
Subject: Re: Changing Z-order with swing components
Date: 1998/08/12
Message-ID: <01bdc5c5$c6cb18b0$1a47aacc@ferd>
X-Deja-AN: 380328375
Content-Transfer-Encoding: quoted-printable
References: <35C96DEF.5737511D@gic-net.com>
Content-Type: text/plain; charset=ISO-8859-1
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.swing