Solved

SWT: wrap Canvas widget

Posted on 2002-05-22
5
722 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_
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 200 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 121
servlet  URL Rewriting 1 36
java stored proc example 9 29
null output 3 24
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

816 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

11 Experts available now in Live!

Get 1:1 Help Now