swt - Extending a class in java but it wont inherit properties from the superclass -
ok on program have labels (which in array , stuff) anyway want add own property inbuilt label
class, anyway tried extending
public class labelmod extends label
but whenever try change creates new type of label instead of default in createcontents()
method doesn't seem inherit original label properties , stack of errors
public void createcontents() { shltictactoe = new shell(); shltictactoe.setsize(450, 300); shltictactoe.settext("tictactoe");
there 9 instances of following (g1x1, g1x2, g1x3, g2x1 etc)
labelmod g1x1 = new labelmod(shltictactoe, swt.none); g1x1.addmouselistener(new mouseadapter() { @override public void mousedown(mouseevent e) { changest(0); } }); g1x1.setimage(swtresourcemanager.getimage(mainform.class, "/res/blank.png")); g1x1.setbounds(10, 10, 64, 64);
and place them in array
createarray(g1x1, g1x2, g1x3, g2x1, g2x2, g2x3, g3x1, g3x2, g3x3);
i using swt create gui , have few labels on main form. why cant create new type of label (with properties added)?
for particular example says "the constructor labelmod(shell, int) undefined"
why dosent inherit stuff superclass? tried adding constuctor labelmod class gives undefined error. error gets fixed in main class however.
public labelmod(shell shltictactoe, int none) { super(shltictactoe,none); // todo auto-generated constructor stub }
another idea had assign of labels labelmods because arent same type complains. hers example of mean:
label g1x1 = new label(shltictactoe, swt.none); g1x1.setimage(swtresourcemanager.getimage(mainform.class, "/res/blank.png")); g1x1.setbounds(10, 10, 64, 64);
and later on have array
public labelmod[] labelarray = new labelmod[9];
and assign label
labelarray[0] = g1x1;
obviously because different types dosent work perhaps possible convert or something?
note: works if use normal label
instead of modified version know thats not problem.
edit:
here class in full if helps
public class labelmod extends label { public labelmod() throws headlessexception { super(); // todo auto-generated constructor stub } public labelmod(string text, int alignment) throws headlessexception { super(text, alignment); // todo auto-generated constructor stub } public labelmod(string text) throws headlessexception { super(text); // todo auto-generated constructor stub } public labelmod(shell shltictactoe, int none) { super(shltictactoe,none); // todo auto-generated constructor stub } public boolean isactivated; public boolean isactivated() { return isactivated; } public void setactivated(boolean isactivated) { this.isactivated = isactivated; } }
also found if hover on original (working) label(shltictactoe, swt.none);
format is
label(composite parent, int style)
edit 2: there errors addmouselistener
says
the method addmouselistener(mouselistener) in type component not applicable arguments (new mouseadapter(){})
and setimage
i undefined again
the method setimage(image) undefined type labelmod
edit 3 1 detail didnt think important vital issue... these imports labelmod
class - keep in mind swt project
import java.awt.headlessexception; import java.awt.label; import org.eclipse.swt.widgets.shell;
edit 4
okay added in appropriate import (swt time - made sure) imports are:
import org.eclipse.swt.widgets.label; import org.eclipse.swt.widgets.shell;
but runtime error, , if go designer view labels dont display here in console:
org.eclipse.swt.swtexception: subclassing not allowed @ org.eclipse.swt.swt.error(swt.java:4282) @ org.eclipse.swt.swt.error(swt.java:4197) @ org.eclipse.swt.swt.error(swt.java:4168) @ org.eclipse.swt.widgets.widget.error(widget.java:468) @ org.eclipse.swt.widgets.widget.checksubclass(widget.java:313) @ org.eclipse.swt.widgets.widget.<init>(widget.java:148) @ org.eclipse.swt.widgets.control.<init>(control.java:110) @ org.eclipse.swt.widgets.label.<init>(label.java:101) @ main.labelmod.<init>(labelmod.java:16) @ main.mainform.createcontents(mainform.java:99) @ main.mainform.open(mainform.java:39) @ main.mainform.main(mainform.java:26)
and in error log ge this:
designer [1.1.0.r37x201109091012.201110120013]: new label(shell, swt.none)
you've got couple of problems here.
first, you're mixing org.eclipse.swt.widgets.label
, java.awt.label
. you're (i think) trying override org.eclipse.swt.widgets.label
, means need have suitable constructor that. means:
public labelmod(shell parent, int style) { super(parent, style); }
you not need of constructors take string
argument. suitable java.awt.label
, not swt.
second, sure want override label
? recommended swt workflow build composite
wraps or contains label
, custom work in wrapping class. there built-in checks you'll need override able subclass swt widget. you'll have override following method:
protected void checksubclass() { /* allow subclass widget. */ }
third, i'm not sure createcontents
method on label
is. since swt wrapper around native window system, different label
s may have different private implementations on different platforms. on mac os, example, there not createcontents
method on label
or of ancestors, indicate private method can't override. you'll need find different method override change behavior you're seeing.
to end, i'll ask again - sure need override label
, not create wrapper around using own composite
? i've been writing swt applications several years , i've had need extend swt widget exactly once, , trivial change, whereas i've created countless composite
s contain or wrap other widgets.
Comments
Post a Comment