Solved

SWT: wrap Canvas widget

Posted on 2002-05-22
5
718 Views
Last Modified: 2013-12-15
I’m going to write some code using the Eclipse SWT.  I want a window that will show a grid of colored squares.  I was thinking I would extend the Canvas widget and draw on that.

In the Programmer’s Guide it suggests wrapping a widget.  “To wrap a widget, you create a subclass of the…Canvas widget”.  Then later it says “Extending a widget by subclassing it is highly discouraged, since it will make your widget dependent on the implementation of the superclass.”  A bit contradictory.  So apparently, I want to wrap the widget without subclassing it.  How?  The design patterns Adaptor and Decorator don’t fit with what I want to do.

0
Comment
Question by:jdaues
5 Comments
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
if I was you, I would just go and extend Canvas.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Why not use a grid of components?
0
 

Author Comment

by:jdaues
Comment Utility
Grid of components:
Having played around with it a little more, I will want not rectangles, but with pixels.  That is, I want to manipulate a grid of pixels, say 500x500, controlling the color of each pixel.  Making each pixel a compoment would be a lot of overhead, I think.  So a grid of components doesn't seem viable with this new setup.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Then try using an approach similiar to what JTable in Swing does.
Have a component that manages the grid, and use a renderer for painting individual cells.
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 200 total points
Comment Utility
From an article entitled "How to Use the Eclipse API"
    http://www.eclipse.org/articles/Article-API%20use/eclipse-api-usage-rules.html
"Only a subset of the API classes were designed to be subclassed. API classes have a subclass contract indicating the terms under which subclasses may be declared. This contract also covers initialization responsibilities and lifecycle responsibilities. Classes that are designed to be subclassed by clients are explicitly flagged in the Javadoc class comment (with words like "Clients may subclass.")."

From "Creating Your Own Widgets Using SWT"
    http://www.eclipse.org/articles/Article-Writing%20Your%20Own%20Widget/Writing%20Your%20Own%20Widget.htm
"Custom widgets are created by subclassing in the existing widget class hierarchy."
"The SWT library provides two widget classes that are typically used as the basis for a custom 100% Java portable widget: Canvas - to create basic widgets, Composite - to create compound widgets."
"To create a custom basic widget, subclass Canvas."
"Imagine we are building an application where we need a widget that displays an image with a line of text to the right ... Since we plan to draw both the image and the text, we subclass Canvas."

And from the javadoc for org.eclipse.swt.widgets.Canvas
    http://www.eclipse.org/documentation/html/plugins/org.eclipse.platform.doc.isv/doc/reference/api/org/eclipse/swt/widgets/Canvas.html

"This class may be subclassed by custom control implementors who are building controls that are not constructed from aggregates of other controls. That is, they are either painted using SWT graphics calls or are handled by native methods."

Evidently then, Canvas is designed to be subclassed given the specified constraints. If your implementation abides by those constraints then I see no reason why you would not directly subclass Canvas as you originally intended.

Best regards,
Jim Cakalic
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now