|
|
|
Re: Why SWT widget can not be implemented? [message #451397 is a reply to message #451370] |
Tue, 01 March 2005 06:18 |
Eclipse User |
|
|
|
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 |
Tiberiu Caprita 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
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04137 seconds