openplanning

Hướng dẫn và ví dụ JavaFX Accordion

Xem thêm các chuyên mục:

Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người.
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.
Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook

1- Javafx Accordion

Trong JavaFX bạn có thể tạo một Accordion bằng cách sử dụng lớp Accordion.
** Accordion **

Accordion root= new Accordion();       
root.getPanes().addAll(firstTitledPane, secondTitledPane);
Tuy nhiên trong thực tế bạn cũng có thể tạo ra một thành phần giao diện giống với Accordion, bằng cách sử dụng kết hợp TitledPaneVBox,
Hình ảnh dưới đây minh họa một nhóm các TitledPane được đặt trong một VBox.

2- Ví dụ với Accordion

AccordionDemo.java

package org.o7planning.javafx.accordion;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class AccordionDemo extends Application {

 
   @Override
   public void start(Stage stage) {


       // Tạo TitledPane thứ nhất (Pane có tiêu đề)
       TitledPane firstTitledPane = new TitledPane();
       firstTitledPane.setText("Java");

       VBox content1 = new VBox();
       content1.getChildren().add(new Label("Java Swing Tutorial"));
       content1.getChildren().add(new Label("JavaFx Tutorial"));
       content1.getChildren().add(new Label("Java IO Tutorial"));

       firstTitledPane.setContent(content1);

       // Tạo TitledPane thứ hai (Pane có tiêu đề)
       TitledPane secondTitledPane = new TitledPane();
       secondTitledPane.setText("CShape");

       VBox content2 = new VBox();
       content2.getChildren().add(new Label("CShape Tutorial for Beginners"));
       content2.getChildren().add(new Label("CShape Enums Tutorial"));

       secondTitledPane.setContent(content2);

     
       Accordion root= new Accordion();      
       root.getPanes().addAll(firstTitledPane, secondTitledPane);
     
       Scene scene = new Scene(root, 300, 200);
       stage.setTitle("Accordion (o7planning.org)");
       stage.setScene(scene);
       stage.show();
   }

   public static void main(String[] args) {
       Application.launch(args);
   }
 
}
Chạy ví dụ:

3- Nhóm các TitledPane

GroupOfTitledPane.java

package org.o7planning.javafx.accordion;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class GroupOfTitledPane extends Application {

  @Override
  public void start(Stage stage) {

      // Tạo TitledPane thứ nhất (Pane có tiêu đề)
      TitledPane firstTitledPane = new TitledPane();
      firstTitledPane.setText("Java");

      VBox content1 = new VBox();
      content1.getChildren().add(new Label("Java Swing Tutorial"));
      content1.getChildren().add(new Label("JavaFx Tutorial"));
      content1.getChildren().add(new Label("Java IO Tutorial"));

      firstTitledPane.setContent(content1);

      // Tạo TitledPane thứ hai (Pane có tiêu đề)
      TitledPane secondTitledPane = new TitledPane();
      secondTitledPane.setText("CShape");

      VBox content2 = new VBox();
      content2.getChildren().add(new Label("CShape Tutorial for Beginners"));
      content2.getChildren().add(new Label("CShape Enums Tutorial"));

      secondTitledPane.setContent(content2);

      // Tạo Pane gốc.
      VBox root = new VBox();
      root.setPadding(new Insets(20, 10, 10, 10));
      root.getChildren().addAll(firstTitledPane, secondTitledPane);
      Scene scene = new Scene(root, 300, 200);

      stage.setTitle("Group Of TitledPane (o7planning.org)");
      stage.setScene(scene);
      stage.show();
  }

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

}
Chạy ví dụ:

Xem thêm các chuyên mục: