3 Replies - 516 Views - Last Post: 05 June 2014 - 04:38 AM Rate Topic: -----

#1 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Change position objects in JavaFX

Posted 04 June 2014 - 07:20 AM

I have some problem. I can't recolate and align the button, the circle or the line where i want it to on the canvas. Dont know why. Would really appricate some help. I want to be able to move the line where i want it, now it seems like all objects are stuck in the middle of the scene or canvas.

package application;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuBuilder;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Timeline extends Application {
    private final int TIMELINE_X_START = 50;
    private final int TIMELINE_X_END = 1150;
    private final int TIMELINE_Y_START = 0;
    private final int TIMELINE_Y_END = 0;


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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Circle circle = timelineCircle();
        Circle circle2 = timelineCircle2();
        Line timeline = timeLine();

        StackPane root = new StackPane();
        Canvas canvas = new Canvas();

        canvas.setWidth(1000);
        canvas.setHeight(700);
        canvas.setCursor(Cursor.DEFAULT);

        Button btn = new Button();
        btn.setText("'New Event'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("The Button Works!");
            }
        });

        btn.setLayoutX(50);
        btn.setLayoutY(50);


        root.getChildren().addAll(timeline,circle,btn,circle2);
        primaryStage.setScene(new Scene(root, 1200, 600));
        primaryStage.show();
    }

    private Line timeLine(){
        Line line = new Line();
        line.setStartX(TIMELINE_X_START);
        line.setStartY(TIMELINE_Y_START);
        line.setEndX(TIMELINE_X_END);
        line.setEndY(TIMELINE_Y_END);
        line.setStrokeWidth(5);
        line.setCursor(Cursor.HAND);
        return line;
    }

    private Circle timelineCircle(){
        Circle circle = new Circle(135,45,40);
        circle.setFill(Color.DODGERBLUE);
        circle.setStroke(Color.BLACK);
        circle.setStrokeWidth(5);
        circle.setCursor(Cursor.HAND);
        return circle;
    }

    private Circle timelineCircle2(){
        Circle circle = new Circle(135,45,40);
        circle.setFill(Color.DODGERBLUE);
        circle.setStroke(Color.BLACK);
        circle.setStrokeWidth(5);
        circle.setCursor(Cursor.HAND);
        return circle;
    }

}


Is This A Good Question/Topic? 0
  • +

Replies To: Change position objects in JavaFX

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 840
  • View blog
  • Posts: 2,576
  • Joined: 29-July 11

Re: Change position objects in JavaFX

Posted 04 June 2014 - 10:51 AM

You are using a StackPane at the layout manager, so yeah, they will stack on top of one another. The position of the StackPane is defaulted to center. You need to use another layout manager. Consider GridPane.
Was This Post Helpful? 2
  • +
  • -

#3 jabbeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 13-January 14

Re: Change position objects in JavaFX

Posted 05 June 2014 - 02:36 AM

View Postfarrell2k, on 04 June 2014 - 10:51 AM, said:

You are using a StackPane at the layout manager, so yeah, they will stack on top of one another. The position of the StackPane is defaulted to center. You need to use another layout manager. Consider GridPane.


Haha yes, thank you, that was the case :) I didnt know that :( I should probably read more about the different Pane's.
Was This Post Helpful? 0
  • +
  • -

#4 Zavael  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 13-May 14

Re: Change position objects in JavaFX

Posted 05 June 2014 - 04:38 AM

yes, you should choose the one that satisfies your needs for example from Oracle docs
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1