Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Why SWT widget can not be implemented?
Why SWT widget can not be implemented? [message #451369] Mon, 28 February 2005 07:04 Go to next message
Eclipse UserFriend
Originally posted by: zhujjie.cn.ibm.com

Button, Label...why?
thx
Re: Why SWT widget can not be implemented? [message #451370 is a reply to message #451369] Mon, 28 February 2005 10:54 Go to previous messageGo to next message
Henning Rogge is currently offline Henning RoggeFriend
Messages: 148
Registered: July 2009
Senior Member
Zhu Jie schrieb:
> Button, Label...why?
> thx

Short question, short answer:
"They can !"...

maybe you should post what kind of error/problem you have.

Henning.
Re: Why SWT widget can not be implemented? [message #451371 is a reply to message #451369] Mon, 28 February 2005 11:10 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
Zhu Jie wrote:

> Button, Label...why?
> thx

When a Programmer says that you shoudn't subclass a specific Class, that
means its internal fields/methods (everything not public) may be changed
(deleted, renamed,...) whithout any notification.

That means your Code may work for a swt Version and later to hang.

Regards,
Tiberiu
PS1 I think there were already discussion about this topic in newsgroup
PS2 Making a Widget:
http://www.eclipse.org/articles/Article-Writing%20Your%20Own %20Widget/Writing%20Your%20Own%20Widget.htm
Re: Why SWT widget can not be implemented? [message #451397 is a reply to message #451370] Tue, 01 March 2005 06:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: zhujjie.cn.ibm.com

Here is my code....
ButtonTest.java
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ButtonTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
MyButton button = new MyButton(shell,SWT.PUSH);
button.setText("Hello,world!");
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

MyButton.java
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class MyButton extends Button {
public MyButton(Composite parent, int style) {
super(parent, style);
}
}

And I got the error message:
org.eclipse.swt.SWTException: Subclassing not allowed
at org.eclipse.swt.SWT.error(SWT.java:2697)
at org.eclipse.swt.SWT.error(SWT.java:2622)
at org.eclipse.swt.SWT.error(SWT.java:2593)
at org.eclipse.swt.widgets.Widget.error(Widget.java:385)
at org.eclipse.swt.widgets.Widget.checkSubclass(Widget.java:260 )
at org.eclipse.swt.widgets.Widget.<init>(Widget.java:110)
at org.eclipse.swt.widgets.Control.<init>(Control.java:89)
at org.eclipse.swt.widgets.Button.<init>(Button.java:102)
at SWT.TEST.MyButton.<init>(MyButton.java:25)
at SWT.TEST.ButtonTest.main(ButtonTest.java:28)
Exception in thread "main"
In SWT help, I found one centense "IMPORTANT: This class is intended to be
subclassed only within the SWT implementation."
Pls help, thx a million!

"Henning Rogge" <HRogge@gmx.net> ???? news:cvute1$bvd$1@www.eclipse.org...
> Zhu Jie schrieb:
> > Button, Label...why?
> > thx
>
> Short question, short answer:
> "They can !"...
>
> maybe you should post what kind of error/problem you have.
>
> Henning.
Re: Why SWT widget can not be implemented? [message #451402 is a reply to message #451397] Tue, 01 March 2005 09:15 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
Zhu Jie wrote:

> Here is my code....
> org.eclipse.swt.SWTException: Subclassing not allowed
> at org.eclipse.swt.widgets.Widget.checkSubclass(Widget.java:260 )
> Pls help, thx a million!

Hi Zhu,
you should still think if it is any sense to hack the SWT.
According SWT, it is useless to subclass its standard Widgets.
If you like your own class, you could use composition
(take a look of "Effective Java Programing" book : "Favor composition
over inheritance" Chapter), something as
class MyButton {private Button swtButton;}

Anyway, just to answer to your question, here is the code that generate
the error (Widget.isValidClass()):
name.substring (0, index + 1).equals (PACKAGE_PREFIX);
static final String PACKAGE_PREFIX = "org.eclipse.swt.widgets.";

That means, you have to create a package "org.eclipse.swt.widgets." and
put there your MyButton.

Tiberiu
Re: Why SWT widget can not be implemented? [message #451404 is a reply to message #451402] Tue, 01 March 2005 09:43 Go to previous messageGo to next message
Tiberiu Caprita is currently offline Tiberiu CapritaFriend
Messages: 68
Registered: July 2009
Member
sure, you can also reimplement
protected void checkSubclass ()
Re: Why SWT widget can not be implemented? [message #451781 is a reply to message #451404] Wed, 09 March 2005 05:29 Go to previous message
Eclipse UserFriend
Originally posted by: zhujjie.cn.ibm.com

You are very helpful, thank you very much!
"Tiberiu Caprita" <capritat@hotmail.com> ????
news:d01dg9$8es$1@www.eclipse.org...
> sure, you can also reimplement
> protected void checkSubclass ()
>
>
Previous Topic:scientific plots in SWT
Next Topic:Link widget and several links
Goto Forum:
  


Current Time: Fri Apr 26 04:13:47 GMT 2024

Powered by FUDForum. Page generated in 0.03567 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top