Nikitin Mikhail
Nikitin Mikhail

Reputation: 3019

How to make javafx label fit text?

I have a dialog window for adding records to the table. As far as I don't want to create for example 10 fxml files for each table I have only one:

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

<?import com.sun.javafx.scene.control.skin.IntegerField?>
<?import javafx.scene.layout.ColumnConstraints?>
<GridPane alignment="BASELINE_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0" prefWidth="300.0" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.{...}.gui.dialogs.AddingDialogController" fx:id="addingDialogPane">
    <columnConstraints>
        <ColumnConstraints hgrow="ALWAYS" minWidth="50.0" prefWidth="100.0" />
        <ColumnConstraints hgrow="ALWAYS" minWidth="50.0" prefWidth="100.0" />        
    </columnConstraints>
    <rowConstraints>        

    </rowConstraints>
    <children>


    </children>
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
    </padding>
</GridPane>

and dinamically add elemnts depending on which kind of record user wants to add. The problem is that when user opens adding dialog he see the following: enter image description here

and that is the problem. Elements covers each other. If I set GridPane's min width to bigger value I get another problem: enter image description here

the big empty space on the left. How to fit elements to the pane correctly without so big empty spaces?

UPD: here is the method used to add elemnts to the pane:

public void setAgent(Agent agent) {

        addingDialogPane.add(AgentNameLabel,1,1);
        addingDialogPane.add(name,2,1);
        addingDialogPane.add(loginLabel, 1,2);
        addingDialogPane.add(login, 2, 2);
        addingDialogPane.add(extUidLabel, 1, 3);
        addingDialogPane.add(extUid, 2, 3);
        addingDialogPane.add(passwordLabel, 1, 4);
        addingDialogPane.add(password, 2, 4);

        addingDialogPane.add(confirm, 1, 5);
        this.agent = agent;
        name.setText(agent.getName());
        login.setText(agent.getLogin());
        extUid.setText(agent.getExt_uid());
        password.setText(agent.getPassword());
    }

Upvotes: 0

Views: 1716

Answers (1)

James_D
James_D

Reputation: 209319

The column (and row) indexing start at 0. So your column constraints apply to columns 0 and 1; you have added elements to columns 1 and 2. So the text fields just get default column constraints, and the empty column 0 is set to grow always. Add elements to columns 0 and 1 and I think it will work.

Upvotes: 1

Related Questions