Search code examples
javajavafxuser-controls

Controls say ... in JavaFX


I'm trying to increase my knowledge on javafx, but running into some troubles with the controls. They are often not wide enough and say ... instead of a desired string. I've attempted to use the setWidth method but this does not work. In this specific case I'm referring to choiceboxes.

My program at the moment

This is a standard javafx program, and I've done this code in the start method. The choicebox is inside of a GridPane. Here's a sample code that recreates the issue.

//imports
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage; 
import javafx.scene.Group;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.*;

//main class
public class HelloWorld extends Application {
//main method
public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    //set title for window
    primaryStage.setTitle("Hello World!");
    //create a new button & format it
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    //give button a set action (print hello world)
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
        }
    });
    //create gridpane to hold button
    GridPane root = new GridPane();

    //establish gridpane
    for(int x = 0; x < 20; x++){
       root.getRowConstraints().add(new RowConstraints(30));
    }
    for(int x = 0; x < 30; x++){
       root.getColumnConstraints().add(new ColumnConstraints(20));
    }
    //set constraints, add button to gridpane
    root.setConstraints(btn,3,3);
    root.getChildren().add(btn);
    //set scene and show
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
 }
}

Solution

  • If you are adding this node to a GridPane, you probably need to extend the node across multiply columns. JavaDocs

    add(Node child, int columnIndex, int rowIndex, int colspan, int rowspan) Adds a child to the gridpane at the specified column,row position and spans.

    gridPane.add(accounts, 0, 0, 2, 1);