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 Pane
a, 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
) ?