Hey could use a little help on a Post-It-Note project I'm just finishing up. It's a small program that only has one other class than Main.
The PostIt class creates a new Undercoated JavaFX Stage and calls stage.show() on creation, making a new post-it. A button in the main class creates objects from this PostIt class and adds them to an ArrayList also in the Main class.
As the window is Undercoated I needed a way to drag the PostIt windows around and I found some code online. This code is commented with "***" in the PostIt class.
So the problem is it dose kind of work... but it mainly just glitches the window around the screen as you try to drag it to a new position. I have no I idea why this is happening...
Main Class:
package com.company; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.control.ComboBox; import javafx.stage.Stage; import javafx.scene.control.Button; import javafx.scene.layout.AnchorPane; import javafx.scene.Scene; import java.util.*; public class Main extends Application{ //Cpntains PostIt Objects used to create new note window ArrayList<PostIt> notes = new ArrayList<PostIt>(); public static void main(String [] args){ launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Winodw Title!!!"); //Button for creating new note and adding it to notes array Button b1 = new Button(); b1.setText("New Note"); //drop down box for selecting color of nate ComboBox comboBox = new ComboBox(); comboBox.getItems().addAll( "Random", "Blue", "Green", "Purple", "Yellow" ); comboBox.getSelectionModel().selectFirst(); //Make new note logic b1.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent e) { //holds new note PostIt newpost = null; //note id int id = 0; //get color from combobox String color; color = (String) comboBox.getValue(); //System.out.println(color); //checks notes array length to calculate ID of new note if (notes.size() != 0){ int temp = notes.size() - 1; id = notes.get(temp).getId() + 1; } try { newpost = new PostIt(primaryStage, id, color); } catch (Exception ex) { ex.printStackTrace(); } //add note to notes list notes.add(newpost); } }); //layout and stage AnchorPane layout = new AnchorPane(); layout.getChildren().add(b1); layout.getChildren().add(comboBox); AnchorPane.setTopAnchor(comboBox, 40.0); Scene scene = new Scene(layout, 300, 250); primaryStage.setScene(scene); primaryStage.show(); } }
PostIt Class:
package com.company; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.TextArea; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.stage.Stage; import javafx.stage.StageStyle; import java.util.Random; public class PostIt extends Application { //post-it stage Stage stage; //ID int id; //used for drag window logic Double xOffset = 0.00; Double yOffset = 0.00; //color logic veriables String color; String hexcolor1; String hexcolor2; public PostIt(Stage primaryStage, int id, String color) throws Exception { //set ID and color this.id = id; this.color = color; //if color is random - roll and set new color if (this.color.equals("Random")){ int rand = getRandomNumberInRange(1,4); System.out.println(rand); if (rand == 1) this.color = "Blue"; else if (rand == 2) this.color = "Green"; else if (rand == 3) this.color = "Purple"; else if (rand == 4) this.color = "Yellow"; } //set color values if (this.color.equals("Blue")){ hexcolor1 = "#73ADEC"; hexcolor2 = "#A0CCF9"; } else if (this.color.equals("Green")){ hexcolor1 = "#C6E951"; hexcolor2 = "#DDF681"; } else if (this.color.equals("Purple")){ hexcolor1 = "#E86BCF"; hexcolor2 = "#FA94E8"; } else if (this.color.equals("Yellow")){ hexcolor1 = "#F0CC40"; hexcolor2 = "#FAE278"; } //start stage stage = new Stage(); stage.initOwner(primaryStage); start(stage); } @Override public void start(Stage stage) throws Exception { stage.initStyle(StageStyle.UNDECORATED); //fix stage size stage.setMaxHeight(400); stage.setMaxWidth(400); stage.setMinHeight(400); stage.setMinWidth(400); stage.setResizable(false); //create textbox and set color - hexcolor2 TextArea textArea = new TextArea(); textArea.setFont(Font.font("Comic Sans MS", 22)); textArea.setWrapText(true); textArea.setStyle("-fx-focus-color: transparent; -fx-text-box-border: transparent;"); textArea.setStyle("-fx-background-color: green"); textArea.setStyle("-fx-control-inner-background:" + hexcolor2 + ";"); //Set layout and color - hexcolor1 AnchorPane pane = new AnchorPane(textArea); pane.styleProperty().set("-fx-background-color: " + hexcolor1); //set textArea location AnchorPane.setTopAnchor(textArea, 80.0); AnchorPane.setLeftAnchor(textArea, 0.0); //*****************Code for dragging window***************** pane.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { xOffset = stage.getX() - event.getSceneX(); yOffset = stage.getY() - event.getScreenY(); } }); pane.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { stage.setX(event.getSceneX() - xOffset); stage.setY(event.getSceneY() - yOffset); } }); //*****************End of code for Dragging window***************** Scene scene = new Scene(pane, 400, 400, Color.BLACK); stage.setScene(scene); stage.show(); //System.out.println("" + getId()); } public int getId() { return id; } public void setId(int id) { this.id = id; } private static int getRandomNumberInRange(int min, int max) { if (min >= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; } }
--- Update ---
Fixed it. I was using getSceanX/Y and not getScreenX/Y. Also there was a small mistake in the math.
pane.setOnMousePressed(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { xOffset = event.getScreenX() - stage.getX(); yOffset = event.getScreenY() - stage.getY(); System.out.println(xOffset); System.out.println(yOffset); } }); pane.setOnMouseDragged(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { stage.setX(event.getScreenX() - xOffset); stage.setY(event.getScreenY() - yOffset); } });