public interface Observer {
public void update();
}
public interface Observable {
public void registerObserver(Observer observer);
public void sendUpdate();
}
@ScriptManifest(name = "LeScript", author = "Token", version = 2.0, info = "", logo = "")
public class Main extends Script implements Observer {
UI ui;
boolean checkBoxValue;
@ Override
public void update() {
checkBoxValue = ui.getCheckBoxValue();
}
@ Override
public void onStart() {
ui = new UI();
}
@ Override
public int onLoop() throws InterruptedException {
return 69;
}
}
@SuppressWarnings("serial")
public class UI extends JFrame implements Observable {
ArrayList<Observer> observers = new ArrayList<Observer>();
@ Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@ Override
public void sendUpdate() {
observers.forEach(o -> o.update());
}
JCheckBox checkBox;
JPanel contentPane;
public boolean getCheckBoxValue() {
return checkBox.isSelected();
}
UI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle("LeTitle");
setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setBounds(100, 100, 582, 401);
contentPane.setLayout(null);
checkBox = new JCheckBox("Checkbox");
checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
sendUpdate();
}
});
checkBox.setBounds(181, 52, 97, 23);
contentPane.add(checkBox);
setVisible(true);
}
}
That's an implementation of the Observer pattern which is used whenever it comes to programming user interfaces, also internally by the swing/awt/javafx libraries and pretty much any other library
You can test it like this
public static void main(String[] args) {
UI ui = new UI();
Observer obs = new Observer() {
@ Override
public void update() {
System.out.println("updated");
}
};
ui.registerObserver(obs);
}
PS: put each class in a separate module