JavaFX Ant Tasks (original) (raw)

Example 10-6 MemoryExample.java

import javafx.application.Application; import javafx.collections.FXCollections; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.TextField; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; import javafx.stage.Stage;

import java.lang.management.ManagementFactory; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences;

public class MemoryTest extends Application {

@Override
public void start(Stage primaryStage) {
    String startMemory = Long.toString(Runtime.getRuntime().totalMemory());
    String maxMemory = Long.toString(Runtime.getRuntime().maxMemory());

    System.out.println("Start memory: " + startMemory);

    System.out.println("Max memory: " + maxMemory);

    final Label startMemoryLabel = new Label("Start memory: ");
    TextField startMemoryTextField = new TextField(startMemory);
    startMemoryTextField.setPromptText(startMemory);

    Label maxMemoryLabel = new Label("Max memory: ");
    final TextField maxMemoryTextField = new TextField(maxMemory);
    maxMemoryTextField.setPromptText(maxMemory);

    Label jvmArgumentsLabel = new Label("JVM Arguments");
    ListView<String> jvmArguments = new ListView<>(FXCollections.observableArrayList(ManagementFactory.getRuntimeMXBean().getInputArguments()));
    jvmArguments.setPrefSize(450, 150);

    Button btn = new Button();
    btn.setText("Update Preferences");
    btn.setOnAction(event -> {
        Preferences prefs = Preferences.userRoot().node(System.getProperty("app.preferences.id")).node("JVMUserOptions");
        String start = startMemoryTextField.getText();
        if (start == null || start.isEmpty()) {
            prefs.remove("-Xms");
        } else {
            prefs.put("-Xms", start);
        }

        String max = maxMemoryTextField.getText();
        if (max == null || max.isEmpty()) {
            prefs.remove("-Xmx");
        } else {
            prefs.put("-Xmx", max);
        }

        try {
            prefs.flush();
        } catch (BackingStoreException e) {
            e.printStackTrace();
        }
    });


    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25, 25, 25, 25));
    grid.getColumnConstraints().setAll(
            new ColumnConstraints(Region.USE_PREF_SIZE, Region.USE_COMPUTED_SIZE, Region.USE_PREF_SIZE, Priority.NEVER, HPos.RIGHT, false),
            new ColumnConstraints(Region.USE_PREF_SIZE, Region.USE_COMPUTED_SIZE, Integer.MAX_VALUE, Priority.ALWAYS, HPos.LEFT, true)
    );

    grid.addRow(0, startMemoryLabel, startMemoryTextField);
    grid.addRow(1, maxMemoryLabel, maxMemoryTextField);
    grid.addRow(2, jvmArgumentsLabel, jvmArguments);
    grid.add(btn, 1, 3);
    grid.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);


    Scene scene = new Scene(grid);

    primaryStage.setTitle("Memory test");
    primaryStage.sizeToScene();
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}