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.
This is the output I feel I should be getting.
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();
}
}
MyGraphPane
import javafx.scene.layout.Pane;
import javafx.scene.input.MouseButton;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import java.util.List;
public class MyGraphPane extends Pane {
private MyGraph graph = new MyGraph();
public MyGraphPane() {
//this.setPrefSize(500, 500);
createGraphics();
//Adds a new vertex to the graph on click
this.setOnMouseClicked(event -> {
if(graph.getSize() == 0 && event.getButton() == MouseButton.PRIMARY){
graph.addVertex(new Vertex(event.getX(), event.getY(), "A"));
}else if(event.getButton() == MouseButton.PRIMARY)
graph.addVertex(new Vertex(event.getX(), event.getY(), graph.getLastName()));
createGraphics();
});
}
private void createGraphics(){
Circle circle;
//Draws the vertices
for(int i = 0; i < graph.getSize(); i++){
circle = new Circle(graph.getVertex(i).getX(), graph.getVertex(i).getY(), 5);
this.getChildren().add(circle);
}
Line line;
int i = 0;
for(List<Integer> list: graph.getEdges()){
for(Integer J: list){
line = new Line(graph.getVertex(i).getX(), graph.getVertex(i).getY(),
graph.getVertex(J).getX(), graph.getVertex(J).getY());
if(!this.getChildren().contains(line)){
this.getChildren().add(line);
}
}
i++;
}
}
public MyGraph getGraph(){
return graph;
}
}
public class Vertex {
private double x;
private double y;
private String name;
public Vertex(double x, double y, String name) {
this.x = x;
this.y = y;
this.name = name;
}
public String getName() {
return name;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
}
package myproject.cmsc315_project4;
import java.util.ArrayList;
import java.util.List;
public class MyGraph{
protected List<Vertex> vertices = new ArrayList<Vertex>();
protected List<List<Integer>> edges = new ArrayList<>();
//Constructor for an empty graph
public MyGraph(){
}
public boolean addVertex(Vertex v){
if(!vertices.contains(v)){
vertices.add(v); //adds the new vertex
edges.add(new ArrayList<>()); //creates a new array list for this vertexes edges
return true;
}else
return false;
}
public boolean addEdge(Vertex a, Vertex b){
if(a.getX() == b.getX() && a.getY() == b.getY()){
throw new IllegalArgumentException("The vertices are the same");
}else if(vertices.contains(a) && vertices.contains(b)){
edges.get(vertices.indexOf(a)).add(vertices.indexOf(b));
return true;
}else
return false;
}
//Get the size of the vertices array list
public int getSize(){
return vertices.size();
}
//Get the name of the last vertex in the list
public String getLastName(){
return vertices.get(getSize()-1).getName();
}
//Get a specific vertex
public Vertex getVertex(int i){
return vertices.get(i);
}
public List<List<Integer>> getEdges(){
return edges;
}
}
I think your issue is that you are adding the edgeBox
to the MyGraphPane
. I think you need to create a root node using VBox
or BorderPane
and add edgeBox
and MyGraphPane
to it.
Main
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;
import javafx.application.Application;
import javafx.scene.layout.VBox;
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();
VBox root = new VBox(edgeBox, pane);
root.setPrefSize(500, 500);
Scene scene = new Scene(root);
stage.setTitle("Project 4");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}