Article View: borland.public.jbuilder.java.swing
Article #774How to retrieve an object deep inside another object?
From: Ramon F Herrera
Date: Thu, 23 Dec 2004 02:26
Date: Thu, 23 Dec 2004 02:26
144 lines
4627 bytes
4627 bytes
This is a problem of some listeners being applied to the
wrong object.
I am trying to write an application which places a
JAI image on top of a JPanel and then performs some
rubberbanding on the image.
I used the standard JBuilder wizard "New Application" and
obviously I have added my own code as you can see below.
This is one of the critical pieces of code:
ScrollingImagePanel sip = new ScrollingImagePanel(img, 950, 645);
frame.getContentPane().add(sip);
MainFrame frame = new MainFrame();
Later on, at the end of jbInit() I have these 2 lines:
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
Those 2 lines allow me to perform the rubberbanding on the
JPanel (without the ScrollingImagePanel on top of it).
So what I need is something like this (I think):
sip.addMouseListener(this);
sip.addMouseMotionListener(this);
However, the variable 'sip' is out of reach at that point.
The question is: given this statement that inserts an object:
frame.getContentPane().add(sip);
How can I retrieve that 'sip' from the object.
I guess am looking for something like this:
sip = frame.getContentPane().retrieveScrollingImagePanel;
Thanks to anyone that can figure this one out...
-Ramon F Herrera
------------------------------------------------------------------
package widgetmaker;
import javax.swing.*;
import javax.media.jai.*;
import javax.media.jai.widget.*;
/**
* <p>Title: WidgetMaker</p>
* <p>Description: Making Widgets by Click & Drag</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: Herrera Creations</p>
* @author Ramon F Herrera
* @version 1.0
*/
public class WidgetMaker {
boolean packFrame = false;
//Construct the application
public WidgetMaker(String fileName) {
RenderedOp img = JAI.create("fileload", fileName);
ScrollingImagePanel sip = new ScrollingImagePanel(img, 950, 645);
MainFrame frame = new MainFrame();
// I wish I could do this: new MainFrame(sip);
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
// Set display name and layout.
frame.setTitle(getClass().getName() + ": " + fileName);
// Display the image.
frame.setLocation(40, 0);
frame.getContentPane().add(sip);
frame.pack();
frame.show();
}
//Main method
public static void main(String[] args) {
String fileName = args[0];
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
new WidgetMaker(args[0]);
}
}
-------------------------------------------------
package widgetmaker;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.media.jai.widget.ScrollingImagePanel;
/**
* <p>Title: WidgetMaker</p>
* <p>Description: Making Widget by Click & Drag</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: Herrera Creations</p>
* @author Ramon F Herrera
* @version 1.0
*/
public class MainFrame
extends JFrame implements MouseListener, MouseMotionListener {
int x, y;
final Stroke[] strokes = new Stroke[10];
int mouseX = 0, mouseY = 0;
int index = 0;
boolean shouldErase;
public ScrollingImagePanel sp;
JPanel contentPane;
[...]
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
Technical Details
Message-ID:
<41ca2cc6$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 111d45,771a2b9d98407c09,start X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Wed, 22 Dec 2004 21:26:16 -0500 From: Ramon F Herrera <ramon@jonjay.com> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: How to retrieve an object deep inside another object? Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.128.127.111 Message-ID: <41ca2cc6$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1103768774 24.128.127.111 (22 Dec 2004 18:26:14 -0800) Lines: 146 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail