shafeen sulaiman
shafeen sulaiman

Reputation: 79

can anyone help me to resize the javaFX scene

I have written code for create, update, delete, and read functions, but now coding a program from a laptop which has a resolution of 1920 x 1080 the window pane or stages looks fine, but if I want to run on another smaller resolution PC or laptop I don't know how to make it responsive or resize the window so all the buttons, fields or image get resized automatically.

Searching on internet I got a code but don't know how to implement it into my FXdocument file.

FXResizeHelper

package ServiceCenter;

import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Screen;
import javafx.stage.Stage;

import java.util.HashMap;

/**
 * @author Simon Reinisch
 * @version 0.0.2
 */
public class FXResizeHelper {

  private final HashMap<Cursor, EventHandler<MouseEvent>> LISTENER = new HashMap<>();
  private final Stage STAGE;
  private final Scene SCENE;
  private final int TR;
  private final int TM;
  private final double SCREEN_WIDTH, SCREEN_HEIGHT;

  private double mPresSceneX, mPresSceneY;
  private double mPresScreeX, mPresScreeY;
  private double mPresStageW, mPresStageH;

  private boolean mIsMaximized = false;
  private double mWidthStore, mHeightStore, mXStore, mYStore;

  /**
   * Create an FXResizeHelper for undecorated JavaFX Stages.
   * The only wish is your job is to create an padding for the Stage so the user can resize it.
   *
   * @param stage - The JavaFX Stage.
   * @param dt    - The area (in px) where the user can drag the window.
   * @param rt    - The area (in px) where the user can resize the window.
   */
  public FXResizeHelper(Stage stage, int dt, int rt) {
    this.TR = rt;
    this.TM = dt + rt;
    this.STAGE = stage;
    this.SCENE = stage.getScene();

    this.SCREEN_HEIGHT = Screen.getPrimary().getVisualBounds().getHeight();
    this.SCREEN_WIDTH = Screen.getPrimary().getVisualBounds().getWidth();

    createListener();
    launch();
  }

  /**
   * Minimize the stage.
   */
  public void minimize() {
    STAGE.setIconified(true);
  }

  /**
   * If the stage is maximized, it will be restored to the last position
   * with height and width. Otherwise it will be maximized to fullscreen.
   */
  public void switchWindowedMode() {
    if (mIsMaximized) {
      STAGE.setY(mYStore);
      STAGE.setX(mXStore);
      STAGE.setWidth(mWidthStore);
      STAGE.setHeight(mHeightStore);
    } else {
      mXStore = STAGE.getX();
      mYStore = STAGE.getY();
      mWidthStore = STAGE.getWidth();
      mHeightStore = STAGE.getHeight();

      STAGE.setY(0);
      STAGE.setX(0);
      STAGE.setWidth(SCREEN_WIDTH);
      STAGE.setHeight(SCREEN_HEIGHT);
    }
    mIsMaximized = !mIsMaximized;
  }

  private void createListener() {
    LISTENER.put(Cursor.NW_RESIZE, event -> {

      double newWidth = mPresStageW - (event.getScreenX() - mPresScreeX);
      double newHeight = mPresStageH - (event.getScreenY() - mPresScreeY);
      if (newHeight > STAGE.getMinHeight()) {
        STAGE.setY(event.getScreenY() - mPresSceneY);
        STAGE.setHeight(newHeight);
      }
      if (newWidth > STAGE.getMinWidth()) {
        STAGE.setX(event.getScreenX() - mPresSceneX);
        STAGE.setWidth(newWidth);
      }
    });

    LISTENER.put(Cursor.NE_RESIZE, event -> {

      double newWidth = mPresStageW - (event.getScreenX() - mPresScreeX);
      double newHeight = mPresStageH + (event.getScreenY() - mPresScreeY);
      if (newHeight > STAGE.getMinHeight()) STAGE.setHeight(newHeight);
      if (newWidth > STAGE.getMinWidth()) {
        STAGE.setX(event.getScreenX() - mPresSceneX);
        STAGE.setWidth(newWidth);
      }
    });

    LISTENER.put(Cursor.SW_RESIZE, event -> {

      double newWidth = mPresStageW + (event.getScreenX() - mPresScreeX);
      double newHeight = mPresStageH - (event.getScreenY() - mPresScreeY);
      if (newHeight > STAGE.getMinHeight()) {
        STAGE.setHeight(newHeight);
        STAGE.setY(event.getScreenY() - mPresSceneY);
      }
      if (newWidth > STAGE.getMinWidth()) STAGE.setWidth(newWidth);
    });

    LISTENER.put(Cursor.SE_RESIZE, event -> {
      double newWidth = mPresStageW + (event.getScreenX() - mPresScreeX);
      double newHeight = mPresStageH + (event.getScreenY() - mPresScreeY);
      if (newHeight > STAGE.getMinHeight()) STAGE.setHeight(newHeight);
      if (newWidth > STAGE.getMinWidth()) STAGE.setWidth(newWidth);
    });

    LISTENER.put(Cursor.E_RESIZE, event -> {
      double newWidth = mPresStageW - (event.getScreenX() - mPresScreeX);
      if (newWidth > STAGE.getMinWidth()) {
        STAGE.setX(event.getScreenX() - mPresSceneX);
        STAGE.setWidth(newWidth);
      }
    });

    LISTENER.put(Cursor.W_RESIZE, event -> {
      double newWidth = mPresStageW + (event.getScreenX() - mPresScreeX);
      if (newWidth > STAGE.getMinWidth()) STAGE.setWidth(newWidth);
    });

    LISTENER.put(Cursor.N_RESIZE, event -> {
      double newHeight = mPresStageH - (event.getScreenY() - mPresScreeY);
      if (newHeight > STAGE.getMinHeight()) {
        STAGE.setY(event.getScreenY() - mPresSceneY);
        STAGE.setHeight(newHeight);
      }
    });

    LISTENER.put(Cursor.S_RESIZE, event -> {
      double newHeight = mPresStageH + (event.getScreenY() - mPresScreeY);
      if (newHeight > STAGE.getMinHeight()) STAGE.setHeight(newHeight);
    });

    LISTENER.put(Cursor.OPEN_HAND, event -> {
      STAGE.setX(event.getScreenX() - mPresSceneX);
      STAGE.setY(event.getScreenY() - mPresSceneY);
    });
  }

  private void launch() {

    SCENE.setOnMousePressed(event -> {
      mPresSceneX = event.getSceneX();
      mPresSceneY = event.getSceneY();

      mPresScreeX = event.getScreenX();
      mPresScreeY = event.getScreenY();

      mPresStageW = STAGE.getWidth();
      mPresStageH = STAGE.getHeight();
    });

    SCENE.setOnMouseMoved(event -> {
      double sx = event.getSceneX();
      double sy = event.getSceneY();

      boolean l_trigger = sx > 0 && sx < TR;
      boolean r_trigger = sx < SCENE.getWidth() && sx > SCENE.getWidth() - TR;
      boolean u_trigger = sy < SCENE.getHeight() && sy > SCENE.getHeight() - TR;
      boolean d_trigger = sy > 0 && sy < TR;

      if (l_trigger && d_trigger) fireAction(Cursor.NW_RESIZE);
      else if (l_trigger && u_trigger) fireAction(Cursor.NE_RESIZE);
      else if (r_trigger && d_trigger) fireAction(Cursor.SW_RESIZE);
      else if (r_trigger && u_trigger) fireAction(Cursor.SE_RESIZE);
      else if (l_trigger) fireAction(Cursor.E_RESIZE);
      else if (r_trigger) fireAction(Cursor.W_RESIZE);
      else if (d_trigger) fireAction(Cursor.N_RESIZE);
      else if (sy < TM && !u_trigger) fireAction(Cursor.OPEN_HAND);
      else if (u_trigger) fireAction(Cursor.S_RESIZE);
      else fireAction(Cursor.DEFAULT);
    });
  }

  private void fireAction(Cursor c) {
    SCENE.setCursor(c);
    if (c != Cursor.DEFAULT) SCENE.setOnMouseDragged(LISTENER.get(c));
    else SCENE.setOnMouseDragged(null);
  }

}

my starter Code

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ServiceCenter;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleButton;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;
import tray.notification.NotificationType;
import tray.notification.TrayNotification;
import tray.animations.AnimationType;

/**
 * FXML Controller class
 *
 * @author Shafeen
 */
public class LoginController implements Initializable {

    @FXML
    private TextField txtUsername;
    @FXML
    private PasswordField password;
    @FXML
    private Button btnLogin;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    @FXML
    private void handleLoginEmployeePayroll(ActionEvent event) {
      
        try {
          

            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));

            Parent root1 = (Parent) fxmlLoader.load();

            root1.setStyle("-fx-background-color:   #2a3747;");
            Stage stage = new Stage();
            FXResizeHelper listener = new FXResizeHelper(stage, 100, 100);
            stage.initStyle(StageStyle.UNDECORATED);

            stage.setTitle("Home");
            stage.setScene(new Scene(root1));
            stage.show();

        } catch (Exception e) {
            System.out.println("Can't load new window");
        }

        Stage stage = (Stage) btnLogin.getScene().getWindow();
        stage.close();
    }

}

Upvotes: 2

Views: 432

Answers (1)

Tag Howard
Tag Howard

Reputation: 322

The easiest thing to do is not to set any hardcoded sizes and instead allow JavaFX to compute sizing for you, if you need to hardcode, setMaxSize() and setMinSize() rather than setting a hardcoded value. If you want your program to maximise by default use Stage.setMaximised(). Unless you are targeting some really old hardware you should be OK.

Upvotes: 2

Related Questions