javajavafxrowgridpane

JavaFX GridPane fixed row size


I am working on a grammar analysis program. I am using a GridPane and have three TextAreas so far. The TextArea paliVerse should span over two rows; the TextArea analysis over four. However, when I change the TextArea analysis with

gridPane.add(analysis,2,1,4,1);
analysis.setPrefRowCount(4);

the TextArea paliVerse also spans over rows, even though the PrefRowCount is 2:

current

How can I change this so that it looks like:

desired

This is the relevant code:

package com.example.projektver180523;

import java.sql.*;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class HelloApplication extends Application {
    private ComboBox<Integer> comboBox;
    private TextArea paliVerse;
    private TextArea paliVerseGER;
    private TextArea analysis;

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

    @Override
    public void start(Stage stage) throws SQLException {

        // Erstelle die ComboBox
        comboBox = new ComboBox<>();

        // Fülle die ComboBox automatisch
        fillComboBox();

        // Erstelle die TextAreas
        paliVerse = new TextArea();
        paliVerseGER = new TextArea();
        analysis = new TextArea();

        // Textfeld ist unveränderlich
        paliVerse.setEditable(false);
        paliVerseGER.setEditable(false);
        analysis.setEditable(false);

        // 2 columns preferred
        paliVerse.setPrefColumnCount(2);
        paliVerseGER.setPrefColumnCount(2);

//        // 2 rows preferred for paliVerse and German translation textArea
        paliVerse.setPrefRowCount(2);
        paliVerseGER.setPrefRowCount(2);
        analysis.setPrefRowCount(4);

        GridPane.setConstraints(comboBox, 0, 1);
        GridPane gridPane = new GridPane();

        // Abstand nach innen setzen
        gridPane.setPadding(new Insets(10));

        // horizontaler Abstand zwischen Kindern
        gridPane.setHgap(10);

        // vertikaler Abstand zwischen Kindern
        gridPane.setVgap(10);

        // Spalte 0 ist 150 weit
        gridPane.getColumnConstraints().add(new ColumnConstraints(230));

        // Spalte 0 ist 480 weit
        gridPane.getColumnConstraints().add(new ColumnConstraints(600));

        gridPane.add(new Label("Vers Nummer:"), 0, 0);
        gridPane.add(comboBox, 1, 0);
        gridPane.add(new Label("Palitext:"), 0, 1);
        gridPane.add(paliVerse, 1, 1);
        gridPane.add(new Label("Übersetzung Deutsch:"), 0, 2);
        gridPane.add(paliVerseGER, 1, 2);
        gridPane.add(new Label("Analyse:"), 2, 0);
        gridPane.add(analysis, 2, 1, 4, 1);
        Scene scene = new Scene(gridPane, 1200, 400);

        // Verknüpfe CSS-Datei
        scene.getStylesheets().add("styles.css");

        // Zeige das Hauptfenster an
        stage.setTitle("Dhammapada Reader");
        stage.setScene(scene);
        stage.show();
    }
}

Solution

  • You seem to be making this more complicated than it needs to be. Each cell in GridPane will be sized to accomodate the Node it contains, as stated in the class documentation:

    By default, rows and columns will be sized to fit their content; a column will be wide enough to accommodate the widest child, a row tall enough to fit the tallest child.

    So all you need do is explicitly set the row span for [TextArea] analysis to 2 (two).
    Consider the following code:
    (Note that the actual contents of the ComboBox is irrelevant to your question so the below code does not populate the ComboBox. Besides, your question doesn't contain the code for method fillComboBox anyway.)

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    
    public class HelloApplication extends Application {
        private ComboBox<Integer> comboBox;
        private TextArea paliVerse;
        private TextArea paliVerseGER;
        private TextArea analysis;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) {
            comboBox = new ComboBox<>();
            paliVerse = new TextArea();
            paliVerseGER = new TextArea();
            analysis = new TextArea();
            GridPane gridPane = new GridPane();
            gridPane.setPadding(new Insets(10));
            gridPane.setHgap(10);
            gridPane.setVgap(10);
            gridPane.add(new Label("Vers Nummer:"), 0, 0);
            gridPane.add(comboBox, 1, 0);
            gridPane.add(new Label("Palitext:"), 0, 1);
            gridPane.add(paliVerse, 1, 1);
            gridPane.add(new Label("Übersetzung Deutsch:"), 0, 2);
            gridPane.add(paliVerseGER, 1, 2);
            gridPane.add(new Label("Analyse:"), 2, 0);
            gridPane.add(analysis, 2, 1, 1, 2);
            Scene scene = new Scene(gridPane, 1200, 400);
    
            // Zeige das Hauptfenster an
            stage.setTitle("Dhammapada Reader");
            stage.setScene(scene);
            stage.show();
        }
    }
    

    This is how it looks:

    screen capture