JavaFX

Overview

  • The launch() method of an Application object starts execution
  • The launch() method is called from main()
  • The start() method of an Application object initializes a Stage object
  • The Stage object is the top-level window
  • The Stage object contains a Scene object
  • The Scene object contains a tree of content nodes

Hello.java

// Hello.java
//    Displays a warm, friendly greeting.

import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class Hello extends Application {

  public void start(Stage stage) {
    // Create a label.
    Label label = new Label("Hello, world!");

    // Create a pane to hold the label.
    BorderPane pane = new BorderPane();
    pane.setCenter(label);

    // Create a scene.
    Scene scene = new Scene(pane, 600, 600);

    // Set up the stage and make it visible.
    stage.setTitle("Hello World!");
    stage.setScene(scene);
    stage.show();
  }

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

Buttons.java

// Buttons.java
//     Demonstrates the use of buttons.

import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class Buttons extends Application {

  public void start(Stage stage) {
    // Create a dialog box.
    Alert alert = new Alert(Alert.AlertType.INFORMATION, "The button was pressed.");
    alert.setTitle("Information Dialog");
    alert.setHeaderText("Information Dialog");
    
    // Create two buttons.
    Button button1 = new Button("Press Me");
    button1.setOnAction(e -> alert.showAndWait());
    Button button2 = new Button("Quit");
    button2.setOnAction(e -> Platform.exit());

    // Create a box to hold the buttons.
    HBox box = new HBox(20);
    box.setPadding(new Insets(20, 20, 20, 20));
    box.getChildren().addAll(button1, button2);

    // Create a scene.
    Scene scene = new Scene(box);

    // Set up the stage and make it visible.
    stage.setTitle("Buttons");
    stage.setScene(scene);
    stage.show();
  }

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

FileView.java

// FileView.java
//     View a text file.

import java.io.*;
import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class FileView extends Application {
  Stage myStage;
  FileChooser chooser;
  TextArea text;
  Alert dialog;
  final static int BUFFER_SIZE = 2048;
  
  void openFile() {
    File file = chooser.showOpenDialog(myStage);
    if (file != null) {
      char[] buffer = new char[BUFFER_SIZE];
      try {
    FileReader in = new FileReader(file);
    int count = 0;
    while ((count = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
      text.appendText(new String(buffer, 0, count));
    }
    text.positionCaret(0);
    myStage.setTitle(file.getName());
      }
      catch (IOException e) {
    dialog.setContentText(e.getMessage());
    dialog.showAndWait();
      }
    }
  }
  
  public void start(Stage stage) {
    myStage = stage;
    
    // Create a message dialog.
    dialog = new Alert(Alert.AlertType.INFORMATION, null);
    dialog.setTitle("File Error");
    dialog.setHeaderText("File Error");

    // Create a file dialog.
    chooser = new FileChooser();
    chooser.setTitle("Open File");
    chooser.setInitialDirectory(new File("."));
    
    // Create the file menu.
    MenuBar menubar = new MenuBar();
    Menu fileMenu = new Menu("File");
    menubar.getMenus().addAll(fileMenu);

    // Add items to the file menu.
    MenuItem openItem = new MenuItem("Open");
    MenuItem exitItem = new MenuItem("Exit");
    openItem.setOnAction(e -> openFile());
    exitItem.setOnAction(e -> Platform.exit());
    fileMenu.getItems().addAll(openItem, exitItem);

    // Create a textarea node.
    text = new TextArea();
    text.setPrefRowCount(50);
    text.setPrefColumnCount(80);
    text.setEditable(false);

    // Create a pane.
    BorderPane pane = new BorderPane();
    pane.setTop(menubar);
    pane.setCenter(text);

    // Create a scene.
    Scene scene = new Scene(pane);

    // Load the stylesheet.
    scene.getStylesheets().add("FileView.css");
    
    // Set up the stage and make it visible.
    stage.setTitle("FileViewer");
    stage.setScene(scene);
    stage.show();
  }

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

FileView.css

.text-area {
    -fx-font: 18px "Monaco", "Sans-Serif";
    -fx-text-fill: white;
}

.text-area .content {
    -fx-background-color: #004;
}