JavaFX BorderPane create a custom control

Description

JavaFX BorderPane create a custom control

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Main extends Application {
   @Override// w  w  w.  java2s .  c  o m
   public void start(Stage primaryStage) {
      DescriptionPane descriptionPane = new DescriptionPane();

      descriptionPane.setTitle("Canada");
      String description = "The Canadian national flag ...";
//      descriptionPane.setImageView(new ImageView("image/ca.gif"));
      descriptionPane.setDescription(description);

      Scene scene = new Scene(descriptionPane, 450, 200);
      primaryStage.setTitle("TextAreaDemo");
      primaryStage.setScene(scene);
      primaryStage.show();
   }

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

class DescriptionPane extends BorderPane {
   private Label lblImageTitle = new Label();

   private TextArea taDescription = new TextArea();

   public DescriptionPane() {
      lblImageTitle.setContentDisplay(ContentDisplay.TOP);
      lblImageTitle.setPrefSize(200, 100);

      lblImageTitle.setFont(new Font("SansSerif", 16));
      taDescription.setFont(new Font("Serif", 14));

      taDescription.setWrapText(true);
      taDescription.setEditable(false);

      ScrollPane scrollPane = new ScrollPane(taDescription);

      setLeft(lblImageTitle);
      setCenter(scrollPane);
      setPadding(new Insets(5, 5, 5, 5));
   }
   public void setTitle(String title) {
      lblImageTitle.setText(title);
   }
   public void setImageView(ImageView icon) {
      lblImageTitle.setGraphic(icon);
   }
   public void setDescription(String text) {
      taDescription.setText(text);
   }
}



PreviousNext

Related