I can’t seem to get the setAlignment method to work to center my HBox of buttons, labels, and textFields. Everything I’ve read online makes me think I’m doing everything right but I think I’m missing something because this isn’t working.
This is the section I’m having problems with.
HBox edgeBox = new HBox();
edgeBox.getChildren().addAll(addEdge);
edgeBox.getChildren().addAll(vertex1Label, vertex1);
edgeBox.getChildren().addAll(vertex2Label, vertex2);
edgeBox.setSpacing(10);
edgeBox.setAlignment(Pos.CENTER);
This is the output I’m getting.
enter image description here
This is the output I feel I should be getting.
enter image description here
This is the full code for the class if it helps. Other elements will be added to the pane when I can figure out my alignment issue. MyGraphPane extends Pane.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.IOException;
public class Project4 extends Application {
@Override
public void start(Stage stage) throws IOException {
Button addEdge = new Button("Add Edge");
Button isConnected = new Button("Is Connected?");
Button hasCycles = new Button("Has Cycles?");
Button dfs = new Button("Depth First Search");
Button bfs = new Button("Breadth First Search");
TextField vertex1 = new TextField();
vertex1.setPrefWidth(30);
Label vertex1Label = new Label("Vertex 1 ");
TextField vertex2 = new TextField();
vertex2.setPrefWidth(30);
Label vertex2Label = new Label("Vertex 2 ");
TextField output = new TextField();
output.setPrefWidth(400);
Label outputLabel = new Label();
HBox edgeBox = new HBox();
edgeBox.getChildren().addAll(addEdge);
edgeBox.getChildren().addAll(vertex1Label, vertex1);
edgeBox.getChildren().addAll(vertex2Label, vertex2);
edgeBox.setSpacing(10);
edgeBox.setAlignment(Pos.CENTER);
MyGraphPane pane = new MyGraphPane();
pane.setPrefSize(500, 500);
pane.getChildren().add(edgeBox);
Scene scene = new Scene(pane);
stage.setTitle("Project 4");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Steven O’Brien is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.