1. Exkurs: JavaFX-Spinner
A Spinner
in JavaFX is a UI control that allows users to select a value from a sequence by either typing it or using arrow buttons to increment/decrement the value.
1.1. Basic Usage
To create a numeric Spinner
:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SpinnerExample extends Application {
@Override
public void start(Stage stage) {
// Create a Spinner for numbers
Spinner<Integer> spinner = new Spinner<>();
// Set the value factory (range: 1 to 100, initial value: 50, step: 1)
spinner.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 100, 50, 1));
// Add spinner to the scene
VBox root = new VBox(10, spinner);
root.setStyle("-fx-padding: 20; -fx-alignment: center;");
Scene scene = new Scene(root, 300, 200);
stage.setScene(scene);
stage.setTitle("JavaFX Spinner Example");
stage.show();
}
public static void main(String[] args) {
launch();
}
}
1.2. Spinner with Strings
A Spinner
can use a list of values, such as strings:
Spinner<String> stringSpinner = new Spinner<>();
SpinnerValueFactory<String> valueFactory =
new SpinnerValueFactory.ListSpinnerValueFactory<>(FXCollections.observableArrayList("Option 1", "Option 2", "Option 3"));
stringSpinner.setValueFactory(valueFactory);
1.3. Event Handling
You can listen to changes in the spinner’s value using a listener:
spinner.valueProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("Selected Value: " + newValue);
});
1.4. Editable Spinner
By default, a Spinner
is editable. You can disable editing as follows:
spinner.setEditable(false);
1.5. Styling the Spinner
The Spinner
can be styled using CSS:
.spinner {
-fx-background-color: lightblue;
-fx-padding: 5;
}
To apply the CSS file:
scene.getStylesheets().add("style.css");
1.6. Advanced Example: Spinner with Different Step Sizes
You can configure a Spinner
with specific step sizes:
Spinner<Double> doubleSpinner = new Spinner<>();
doubleSpinner.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0.0, 10.0, 5.0, 0.5));
1.7. Common Methods
-
getValue()
: Retrieves the current value of the spinner. -
setValue()
: Sets a specific value for the spinner. -
increment(int steps)
: Increases the spinner’s value by a specified number of steps. -
decrement(int steps)
: Decreases the spinner’s value by a specified number of steps.
1.8. References
-
JavaFX API Reference: Spinner Documentation
-
JavaFX Tutorials: CodeMakery JavaFX Tutorials
This content was createt with ChatGPT (Prompt: How to to use spinner in JavaFx) |
2. 2025-03-25
2.1. Sockets
var serverSocket = ServerSocketFactory
.getDefault()
.createServerSocket(8080);
-
Handler für einkommende Requests (ClientSocket)
Socket clientSocket = serverSocket.accept(); handleConnect(clientSocket);
-
Streams für Input und Output
var outputStream = clientSocket.getOutputStream(); var inputStream = clientSocket.getInputStream();