Re: GUIKit - ScrollPane Tables within Wizard
- To: mathgroup at smc.vnet.net
- Subject: [mg53060] Re: GUIKit - ScrollPane Tables within Wizard
- From: Jeff Adams <jeffa at wolfram.com>
- Date: Wed, 22 Dec 2004 04:52:51 -0500 (EST)
- References: <200412150926.EAA10576@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Dec 15, 2004, at 3:26 AM, paulw at cwgsy.net wrote: > The second problem I have found is that when you attempt to include an > image within the table, it outputs some script along the lines of > JavaObject##########Released where ###'s are replaced with the object > id number. This does not happen using the exact same code outside of > the wizard. Hello, To help track down possible sources of getting Removed[JavaObjects] in your use of GUIKit I thought it might be easiest to explain what could be going on with a short example definition. Hopefully some of this insight into the workings of objects in GUIKit and JLink will aid in debugging any temporary coding problems you might have during development. Typically one might create an image and set it as the icon of a button at the time of creating the button within the definition of the button, like so: Needs["GUIKit`"] GUIRun[ Widget["Panel", { Widget["Label", {"Text" -> "Icon reference demo:"}], Widget["Button", {"Icon" -> Widget["Icon", {"Data" -> Script[ ExportString[ ToBoxes[a + b/c^4], "GIF", ConversionOptions -> {"Transparency" -> GrayLevel[1]}]]}]}] }] ] But notice what happens below if we try and create the icon widget in one script and assign it to a Mathematica symbol and then try to use it in the creation of the button sometime later on: GUIRun[ Widget["Panel", { Widget["Label", {"Text" -> "Icon reference demo:"}], Script[ icn = Widget["Icon", {"Data" -> Script[ ExportString[ ToBoxes[a + b/c^4], "GIF", ConversionOptions -> {"Transparency" -> GrayLevel[1]}]]}];], Widget["Button", {"Icon" -> Script[Print[icn]; icn]}] }] ] From In[]:=Removed[JavaObject241387681349633] Out[]=$Failed This fails to work because the icon object that is created in the first Script is not retained by the system because all GUIKit Script[] blocks are automatically wrapped in a JavaBlock[]. You can see more documentation of JavaBlock in the J/Link Help Browser documentation but it essentially releases objects, created from calls in Mathematica but not referenced in Java, from leaking, and so within the second Script call, this JavaObject is already removed from the system (Java VM). The recommended way of creating and managing the lifetime of widget objects in GUIKit is naming them either on creation using the Name option or alternately naming them separately using SetWidgetReference["name", obj_]. This not only manages the lifetime of the objects automatically for you by GUIKit until the associated window is closed, but also allows you to reference these named objects using all the other existing GUIKit functions including the function WidgetReference["name"]. Below are two variants of the same functionality. Once uses the Name -> _ option of Widget, the other separately registers the object using a name through the SetWidgetReference["name", obj_] function. Note that the button is then created using the existing icon widget instance created in the first Script block: This technique uses the Name -> _ option GUIRun[ Widget["Panel", { Widget["Label", {"Text" -> "Icon reference demo:"}], Script[ Widget["Icon", { "Data" -> Script[ ExportString[ ToBoxes[a + b/c^4], "GIF", ConversionOptions -> {"Transparency" -> GrayLevel[1]}]]}, Name -> "myIcon"]; ], Widget["Button", {"Icon" -> WidgetReference["myIcon"]}] }] ] this second version instead uses the SetWidgetReference function which can be useful if you make separate decisions on registering objects from when they are actually created by the code: GUIRun[ Widget["Panel", { Widget["Label", {"Text" -> "Icon reference demo:"}], Script[ icn = Widget["Icon", {"Data" -> Script[ ExportString[ ToBoxes[a + b/c^4], "GIF", ConversionOptions -> {"Transparency" -> GrayLevel[1]}]]}]; SetWidgetReference["myIcon", icn]; ], Widget["Button", {"Icon" -> WidgetReference["myIcon"]}] }] ] For those of you familiar with J/Link you might think that using KeepJavaObject would also work to make sure these objects are available. And although this does work, it is not as clean a technique as using the named object system of GUIKit. For example if you were to try using just KeepJavaObject: GUIRun[ Widget["Panel", { Widget["Label", {"Text" -> "Icon reference demo:"}], Script[ icn = Widget["Icon", { "Data" -> Script[ ExportString[ ToBoxes[a + b/c^4], "GIF", ConversionOptions -> {"Transparency" -> GrayLevel[1]}]]}]; KeepJavaObject[icn]; ], Widget["Button", {"Icon" -> Script[icn]}] }] ] This works, but if you check the J/Link function PeekObjects[] you will see this icon leaks into the system and stays around even after the GUIKit window is closed. To fix this you would actually have to use ReleaseJavaObject at the appropriate place: GUIRun[ Widget["Panel", { Widget["Label", {"Text" -> "Icon reference demo:"}], Script[ icn = Widget["Icon", {"Data" -> Script[ ExportString[ ToBoxes[a + b/c^4], "GIF", ConversionOptions -> {"Transparency" -> GrayLevel[1]}]]}]; KeepJavaObject[icn]; ], Widget["Button", {"Icon" -> Script[icn], Script[ReleaseJavaObject[icn]]}] }] ] And this is definitely not as convenient as the named object versions recommended previously. Jeff Adams Wolfram Research
- References:
- GUIKit - ScrollPane Tables within Wizard
- From: paulw@cwgsy.net
- GUIKit - ScrollPane Tables within Wizard