JavaFX dziedziczenie z GridPane a wyświetlanie

0

Witajcie!

Piszę sobie Sudoku, napisałem taką oto klasę:

public class SudokuBoard extends GridPane{

	private GridPane grid;
	
	public SudokuBoard(double prefWidth, double prefHeight) {
		createBoard(prefWidth, prefHeight);
	}
	
	public SudokuBoard(double prefSize) {
		createBoard(prefSize, prefSize);
	}
	
	public SudokuBoard() {
		createBoard(500, 500);
	}
	
	private void createBoard(double prefWidth, double prefHeight) {
		grid = new GridPane();
		
		grid.setMaxSize(prefWidth, prefHeight);
		
		int columnsNum ,rowsNum;
		columnsNum = rowsNum = 9;
		
		for (int row = 0; row < rowsNum; row++){
            RowConstraints rc = new RowConstraints();
            rc.setFillHeight(true);
            rc.setVgrow(Priority.ALWAYS);
            grid.getRowConstraints().add(rc);
        }
		
		for (int col = 0; col < columnsNum; col++) {
            ColumnConstraints cc = new ColumnConstraints();
            cc.setFillWidth(true);
            cc.setHgrow(Priority.ALWAYS);
            grid.getColumnConstraints().add(cc);
		}
		
		fillGrid();
	}
	
	private void fillGrid() {
		for (int i = 0 ; i < 81 ; i++) {
			grid.add(createTextArea(), i % 9, i / 9);
	    }
	}
	
	private TextArea createTextArea() {
		TextArea text = new TextArea();
		text.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        return text;
    }
	
	//Here is the getter!
	public GridPane getBoard() {
		return grid;
	}
}

Jednakże kiedy staram się wyświetlić obiekt SudokuBoard w takim przykładowym kodzie:

BorderPane window = new BorderPane();
		window.setPrefSize(600, 600);
		window.setVisible(true);
		SudokuBoard board = new SudokuBoard();
		window.setCenter(board);

nie wyświetla się nic. Dopiero dopisanie gettera do zwracania obiektu GridPane pozwala na odpowiednie wyświetlenie danego Panea, czyli: window.setCenter(board.getBoard());

Pytanie brzmi: czy jestem w stanie, a jeśli tak to jak, sprawić aby klasa SudokuBoard zachowywała się jak pełnoprawny Pane (tj. wyświetlała się po dodaniu do sceny bez użycia gettera) ?

1

Najpierw się zdecyduj czy chcesz użyć delegacji czy dziedziczenia bo na razie ze średnim skutkiem używasz obu.
Poprawione na dziedziczenie może wyglądać tak:

public class SudokuBoard extends GridPane{
	 
    public SudokuBoard(double prefWidth, double prefHeight) {
		super();
        createBoard(prefWidth, prefHeight);
    }
 
    public SudokuBoard(double prefSize) {
		super();
        createBoard(prefSize, prefSize);
    }
 
    public SudokuBoard() {
		super();
        createBoard(500, 500);
    }
 
    private void createBoard(double prefWidth, double prefHeight) {
 
        this.setMaxSize(prefWidth, prefHeight);
 
        int columnsNum ,rowsNum;
        columnsNum = rowsNum = 9;
 
        for (int row = 0; row < rowsNum; row++){
            RowConstraints rc = new RowConstraints();
            rc.setFillHeight(true);
            rc.setVgrow(Priority.ALWAYS);
            this.getRowConstraints().add(rc);
        }
 
        for (int col = 0; col < columnsNum; col++) {
            ColumnConstraints cc = new ColumnConstraints();
            cc.setFillWidth(true);
            cc.setHgrow(Priority.ALWAYS);
            this.getColumnConstraints().add(cc);
        }
 
        fillGrid();
    }
 
    private void fillGrid() {
        for (int i = 0 ; i < 81 ; i++) {
            this.add(createTextArea(), i % 9, i / 9);
        }
    }
 
    private TextArea createTextArea() {
        TextArea text = new TextArea();
        text.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
        return text;
    }
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1