?
Solved

SWT: wrap Canvas widget

Posted on 2002-05-22
5
Medium Priority
?
730 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 16

Expert Comment

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

Expert Comment

by:objects
ID: 7028708
Why not use a grid of components?
0
 

Author Comment

by:jdaues
ID: 7029646
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
ID: 7030748
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 800 total points
ID: 7042613
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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