본문 바로가기

JavaFX

[JavaFX] JavaFX 공통 페이징 처리 목록 만들기

JavaFX 공통 페이징 처리 목록 만들기

 

JavaFX에서 게시글 페이징 처리하기 위해 JavaFX의 Pagination을 사용해봤다.

 

 

 

fxml로 view화면을 만들고 게시글 목록을 페이징할 수 있도록 AnchorPane을 생성했다.

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="700.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.NoticeController">
   <children>
      <AnchorPane fx:id="notice" layoutX="14.0" layoutY="14.0" prefHeight="607.0" prefWidth="524.0" />
      <Label layoutX="244.0" layoutY="633.0" text="리스트">
         <font>
            <Font size="16.0" />
         </font>
      </Label>
      <AnchorPane layoutX="797.0" layoutY="124.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.leftAnchor="797.0" />
   </children>
</AnchorPane>

 

 

view화면을 컨트롤하기 위해 컨트롤러를 하나 생성한다.

Paging은 클래스로 빼서 공통으로 사용할 수 있도록 따로 Paging 클래스를 만들었다.

initialize에서 Paging객체를 생성하고 call 이벤트 함수를 정의한다.

페이징 클릭 이벤트 발생 시 call이 호출되고 cratePage 메소드에서 데이터를 조회하여 VBox로 데이터를 출력하게 된다.

 

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.util.Callback;

public class NoticeController implements Initializable{

	String[] fonts = new String[]{};
	
	Paging paging;
	
	@FXML
	private AnchorPane notice;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		
		fonts = Font.getFamilies().toArray(fonts);
		paging = new Paging(fonts.length/3, 0);
		paging.setPageSize(15);
		paging.getPagination().setPageFactory(new Callback<Integer, Node>() {
          @Override
          public Node call(Integer pageIndex) {
        	  System.out.println(pageIndex);
              return createPage(pageIndex);               
          }
		});
		
		notice.getChildren().add(paging.getAnchor());
	}
	
	public VBox createPage(int pageIndex) {        
        VBox box = new VBox(5);
        int page = pageIndex * paging.getPageSize();
        
        // Data 조회
        
        for (int i = page; i < page + paging.getPageSize(); i++) {
        	Label font = new Label(fonts[i]);
            Hyperlink link = new Hyperlink(font.getText());
            
        	box.getChildren().add(font);
        }
        return box;
    }

	
	
}

 

 

 

페이징 처리하기 위해 Pagination을 상속했다.

기본적인 페이징 스타일을 초기화 하고 페이징 객체를 생성하게 된다.

 

package application;

import javafx.scene.control.Pagination;
import javafx.scene.layout.AnchorPane;

public class Paging extends Pagination{

	private Pagination pagination;
	
	/** 페이징 AnchorPane **/
	private AnchorPane anchor;
	
	/** 한 페이지당 게시글 수 **/
	private int pageSize = 5;
	
	public Paging(int totalSize, int curPage) {
		// 목록의 총 갯수 / 현재 페이지 인덱스
		pagination = new Pagination(totalSize/getPageSize(), curPage);
		
		setStyle();
	}
	
	public void setStyle() {
		pagination.setStyle("-fx-border-color:#ccc;");
		
		anchor = new AnchorPane();
        AnchorPane.setTopAnchor(pagination, 10.0);
        AnchorPane.setRightAnchor(pagination, 10.0);
        AnchorPane.setBottomAnchor(pagination, 10.0);
        AnchorPane.setLeftAnchor(pagination, 10.0);
        anchor.setStyle("-fx-padding: 30px; -fx-pref-width: 500px; -fx-pref-height: 600px;");
        anchor.getChildren().addAll(pagination);
	}
	
	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
	
	public AnchorPane getAnchor() {
		return anchor;
	}
	
	// 한페이지에 보여줄 목록 갯수
	public int getPageSize() {
		return this.pageSize;
	}
	
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
}