<sub class="descriptionSection">13-05-2025 05:20:pm // #Tag // [[../Programmierung|Programmierung]]</sub>
____
## TextField
### TextField EventListener
```java
TextField textField = new TextField();
textField.textProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("textfield changed from " + oldValue + " to " + newValue);
});
```
## EventHandler
EventHandler Klassen müssen das Interface `EventHandler` implementieren.
```java
class HandleCoolEvent implements EventHandler<TypeOfEvent>{
@override
public void handle(TypeOfEvent event){
//Do some fancy stuff
}
}
```
Event Handler werden so registriert:
```java
Button testButton = new Button();
testButton.addEventListener(ActionEvent, new CoolEventListener());
//Oder direkte implementierung:
RadioButton Selector = new RadioButton("Testing");
Selector.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Octal selected");
setCalcMode(CalcMode.OCTAL);
}
});
```
## Exceptions
Exception Classes müssen entweder Exception oder RuntimeException extenden
```java
class MyCoolException extends RuntimeException{
MyCoolException(String msg){
super(msg)
}
}
```
RuntimeExceptions müssen nicht deklariert oder gehandled werden, Exceptions müssen deklariert und gehandelt werden:
```java
class TestClass{
void testing() throws SomeException{
throw new SomeException("I hate Java");
}
}
```
## WindowClass
```java
package gui.gui;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList;
public class Window {
private Parent root;
private Stage stage;
private Scene scene;
private double width;
private double height;
private String title;
ArrayList<Node> children = new ArrayList<Node>();
Window(double width, double height, String title, boolean isResizable) {
this.root = new VBox();
this.scene = new Scene(this.root, width, height);
this.stage = new Stage();
this.stage.setScene(this.scene);
this.stage.setWidth(width);
this.stage.setHeight(height);
this.stage.setResizable(isResizable);
this.stage.setTitle(title);
this.width = width;
this.height = height;
this.title = title;
this.registerInitialEventHandlers();
}
public void setRoot(Parent root) {
this.root = root;
this.scene.setRoot(root);
}
public void addChild(Node child){
this.children.add(child);
this.reRenderChildren();
}
public void removeChild(Node child){
this.children.remove(child);
this.reRenderChildren();
}
public ArrayList<Node> getChildren(){
return this.children;
}
public void closeStage(){
this.stage.close();
}
public void showStage(){
this.stage.show();
}
private void registerInitialEventHandlers(){
this.stage.setOnCloseRequest(event -> {
System.out.println("Stage is closing");
});
this.stage.setOnShown(event -> {
System.out.println("Stage is shown");
});
this.stage.widthProperty().addListener((observable, oldWidth, newWidth)->{
this.width = newWidth.doubleValue();
});
this.stage.heightProperty().addListener((observable, oldHeight, newHeight)->{
this.height = newHeight.doubleValue();
});
}
private void reRenderChildren(){
if(this.root instanceof Pane){
((Pane)this.root).getChildren().clear();
((Pane)this.root).getChildren().addAll(this.children);
}
else{
System.out.println("Root is not a VBox");
}
}
}
```
## Files
```java
//Inhalt eines Files zeile für zeile ausgeben
//Braucht
// java.nio.file für Path nicht javafx.scene.shape
try{
Path pfad = Paths.get("D:/text.txt");
for(String zeile: Files.readAllLines(pfad)){
System.out.println(zeile);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
```
## Scanner
```java
//import java.util.Scanner;
Scanner myObj = new Scanner(System.in);
System.out.println("Enter Username");
String userName = myObj.nextLine();
System.out.println("Username is: ", userName);
//Andere Scanner Methoden
myObj.nextInt();
myObj.nextDouble();
```
## Random
```java
Random rng = new Random(Max);
int randomNumber = rng.nextInt();
```