Come da titolo vorrei creare un componente personalizzato per la mia interfaccia fatta con Java FX e FXML quindi ho creato il codice del componente:
codice:
public class RedTextField extends TextField implements EventHandler{
public static final int CONTENT_STRING = 1;
public static final int CONTENT_NUMBER = 2;
public static final int CONTENT_NUMBER_POSITIVE = 3;
public static final int CONTENT_NUMBER_CLOSED_POSITIVE = 4;
public static final int CONTENT_NUMBER_ZERO_ONE = 5;
public static final int CONTENT_NUMBER_ZERO_HUNDRED = 6;
public static final int CONTENT_NUMBER_STRICTLY_ZERO_ONE = 7;
private int content = CONTENT_NUMBER_CLOSED_POSITIVE;
private boolean isOkNumber = true;
private double value = 0;
public RedTextField(int content){
this();
this.content = content;
}
public RedTextField() {
this("");
}
public RedTextField(String text) {
super(text);
this.setEventHandler(EventType.ROOT, this);
}
public void setContentType(int i){
this.content = i;
}
@Override
public void handle(Event event) {
javafx.scene.text.Font old = this.getFont();
switch(this.content){
case CONTENT_STRING:
this.isOkNumber = true;
break;
case CONTENT_NUMBER:
case CONTENT_NUMBER_POSITIVE:
case CONTENT_NUMBER_ZERO_ONE:
case CONTENT_NUMBER_CLOSED_POSITIVE:
case CONTENT_NUMBER_ZERO_HUNDRED:
case CONTENT_NUMBER_STRICTLY_ZERO_ONE:
try{
this.isOkNumber = true;
value = this.isEmpty() ? 0 : Double.parseDouble((String)this.getText());
switch(this.content){
case CONTENT_NUMBER_POSITIVE:
if(value<0) throw new Exception();
break;
case CONTENT_NUMBER_CLOSED_POSITIVE:
if(value<=0) throw new Exception();
break;
case CONTENT_NUMBER_ZERO_ONE:
if(value<0 || value >1) throw new Exception();
break;
case CONTENT_NUMBER_STRICTLY_ZERO_ONE:
if(value<=0 || value >=1) throw new Exception();
break;
case CONTENT_NUMBER_ZERO_HUNDRED:
if(value<0 || value >100) throw new Exception();
break;
}
System.out.println("OKOKOK");
//this.setBackground(Background.EMPTY);
//this.setFont(new Font(old.getName(), Font.PLAIN,old.getSize()));
}catch(Exception e){
System.out.println("Eccezione");
//this.setForeground(Color.RED);
//this.setFont(new Font(old.getName(), Font.BOLD,old.getSize()));
this.isOkNumber = false;
}
break;
}
}
public boolean isOkNumber(){
if(this.isEmpty()) return false;
return isOkNumber;
}
public boolean isOkNumberEmptyAllowed(){
return isOkNumber;
}
public boolean isOkNumberEnabled() {
if(!this.isEditable()) return true;
else return this.isOkNumber();
}
public boolean isEmpty(){
String s = this.getText();
s = s.trim();
return s.isEmpty() || s.equalsIgnoreCase("");
}
}
e nel mio file FXML ho agguinto (suggerito da NetBeans)
codice:
<JavaFXComponents.RedTextField />
però ottengo questo errore
codice:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:367)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:305)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:894)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: JavaFXComponents.RedTextField is not a valid type.
unknown path:18
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2613)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2790)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2720)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at AAA.NewFXMain.start(NewFXMain.java:29)
at com.sun.javafx.application.LauncherImpl$8.run(LauncherImpl.java:837)
at com.sun.javafx.application.PlatformImpl$7.run(PlatformImpl.java:335)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:301)
at com.sun.javafx.application.PlatformImpl$6$1.run(PlatformImpl.java:298)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(PlatformImpl.java:298)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
... 1 more
Exception running application AAA.NewFXMain
Java Result: 1
Non capisco proprio come fare.... cosa sbaglio?