Solved

SWT: wrap Canvas widget

Posted on 2002-05-22
5
719 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
best (free) software to access postgres db (java) 1 33
JDeveloper 12c for 32 bit 4 71
get weblogic logged in user in java 2 58
JList custom Cell Renderer refresh 15 53
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

863 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

18 Experts available now in Live!

Get 1:1 Help Now