UPDATE 2019.05.14 4:54PM EST - OK - here is code that illustrates my issue - probably took me way too long to get this, and it's probably too lengthy, but again, I'm new to Java. Anyway - it works, brings the form up, and fails to have the second button see and react to the "Event". I think i am now "raising" the event OK - at least it is hitting the code in the event. However, it is still running the class code and not the "custom" code passed in via setOnFormStateChange ???? I'm not sure what's wrong.
All Imports
import java.util.ArrayList;
import java.util.Date;
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
MyApp.java
public class MyApp extends Application {
public static void main(String[] args) {
launch(args); // this method will hang here until main form is closed!
}
@Override
public void start(Stage stage) throws Exception {
Form myForm = new Form();
myForm.ShowForm();
}
}
Form.java
class Form {
private boolean modified;
private ArrayList<FormStateChangeListener> registry = new ArrayList<>();
public void setModified(boolean m) {
modified = m;
this.throwStateChange(m);
}
public void throwStateChange(boolean m) {
for (FormStateChangeListener o : registry) {
// o.onFormStateChange(m);
FormStateChangeEvent.fireEvent(o,m);
}
}
public void ShowForm() {
Label lbl = new Label("NORMAL STATE");
lbl.setLayoutX(50);
lbl.setLayoutY(20);
Btn myBtn1 = new Btn(this);
myBtn1.setLayoutX(100);
myBtn1.setLayoutY(100);
myBtn1.setPrefWidth(200);
myBtn1.setText("Press To Change State");
myBtn1.setOnAction(e -> {
lbl.setText("CHANGED STATE:" + new Date().toString());
this.setModified(true);
});
Btn myBtn2 = new Btn(this);
myBtn2.setLayoutX(100);
myBtn2.setLayoutY(200);
myBtn2.setPrefWidth(200);
myBtn2.setText("And This Should React");
myBtn2.setOnFormStateChange(e -> {
myBtn2.setText("I REACTED!");
});
Stage stage = new Stage();
AnchorPane root = new AnchorPane();
Scene scene = new Scene(root, 430, 400);
// root.getChildren().add(lbl);
// root.getChildren().add(myBtn1);
// root.getChildren().add(myBtn2);
root.getChildren().addAll(lbl,myBtn1,myBtn2);
stage.setScene(scene);
stage.showAndWait();
}
void registerForEvent(FormStateChangeListener t) {
registry.add(t);
}
}
FormStateChangeListener.java
interface FormStateChangeListener {
public void onFormStateChange(boolean mod);
}
FormState.java
enum FormState {
NORMAL, MODIFIED, NEW
}
Btn.java
class Btn extends Button implements FormStateChangeListener {
private final ObjectProperty<EventHandler<? super FormStateChangeEvent>> onFormStateChange
= new SimpleObjectProperty<EventHandler<? super FormStateChangeEvent>>(this, "onFormStateChange") {
@Override
protected void invalidated() {
setEventHandler(FormStateChangeEvent.FORM_STATE_CHANGE, get());
}
};
public Btn(Form f) { // constructor
f.registerForEvent(this); // register for the event
}
public final void setOnFormStateChange(EventHandler<? super FormStateChangeEvent> handler) {
onFormStateChange.set(handler);
}
public final EventHandler<? super FormStateChangeEvent> getOnFormStateChange() {
return onFormStateChange.get();
}
public final ObjectProperty<EventHandler<? super FormStateChangeEvent>> onFormStateChangeProperty() {
return onFormStateChange;
}
public void onFormStateChange(boolean mod) {
//in reality nothing would be here, but is just for testing
System.out.println("Code from class.");
}
;
}
FormStateChangeEvent.java
class FormStateChangeEvent extends Event {
public static final EventType<FormStateChangeEvent> ANY = new EventType<>(Event.ANY, "MY_EVENT");
public static final EventType<FormStateChangeEvent> FORM_STATE_CHANGE = new EventType<>(ANY, "FORM_STATE_CHANGE");
static void fireEvent(FormStateChangeListener o, boolean mod) {
//throw new UnsupportedOperationException("Not supported yet.");
o.onFormStateChange(mod);
}
public FormStateChangeEvent(EventType<? extends FormStateChangeEvent> eventType) {
super(eventType);
}
}