Using other Class in Scene

79 Views Asked by At

guys, Im new in JavaFX, so I know this is going to be stupid(or easy to solve), but I created a Togglebuttons menu in one class, that I would like to put it to the Main.

My LeftMenu class looks like this:

package application;

import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

public class LeftMenu extends BorderPane{

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

        public LeftMenu() {

    ToggleGroup group = new ToggleGroup();


    ToggleButton sr = new ToggleButton("SR");
    sr.setTooltip(new Tooltip("SR"));
    sr.setPrefWidth(40);
    sr.setPrefHeight(40);
    sr.setOnAction(e-> {
        if(sr.isSelected())
        System.out.println(sr.getText()  + " ON");
        else
        System.out.println(sr.getText() + " OFF");
    });


    ToggleButton mh = new ToggleButton("MH");
    mh.setTooltip(new Tooltip("MH"));
    mh.setPrefWidth(40);
    mh.setPrefHeight(40);
    mh.setOnAction(e-> {
        if(mh.isSelected())
        System.out.println(mh.getText()  + " ON");
        else
        System.out.println(mh.getText() + " OFF");
    });

    ToggleButton ssfha = new ToggleButton("SSFHA");
    ssfha.setTooltip(new Tooltip("SSFHA"));
    ssfha.setPrefWidth(40);
    ssfha.setPrefHeight(40);
    ssfha.setOnAction(e-> {
        if(ssfha.isSelected())
        System.out.println(ssfha.getText()  + " ON");
        else
        System.out.println(ssfha.getText() + " OFF");
    });

    sr.setToggleGroup(group);
    mh.setToggleGroup(group);
    ssfha.setToggleGroup(group);

    //Menu
    VBox level = new VBox();
    level.getStyleClass().addAll("vbox");
    level.getChildren().addAll(sr, mh, ssfha);

 }
}

And I would like to put it in here (I dont have any errors, so I dont know whats the case):

package application;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class Main extends Application {

Stage window;

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

@Override
public void start(Stage primaryStage) throws Exception{
    {
        Stage window = primaryStage;
        window.setTitle("Menu Test");
        window.setMaximized(true);

        StackPane root = new StackPane();

        BorderPane lMenu = new LeftMenu();          
        BorderPane rightPane = new BorderPane();

        root.getChildren().addAll(lMenu,rightPane);

        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        window.setScene(scene);
        window.show();

    }
}
}

Thank you for help.

1

There are 1 best solutions below

0
On

First, use an appropriate layout pane that allows you to see both child nodes at once, instead of a StackPane that places one on top of the other (obscuring the bottom one):

// StackPane root = new StackPane();
HBox root = new HBox();

Second, you don't seem to add any content to your custom class:

import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

public class LeftMenu extends BorderPane{

    // why on earth is this here? Get rid of it:
    //public static void main(String[] args) {
    //}

    public LeftMenu() {

        ToggleGroup group = new ToggleGroup();


        ToggleButton sr = new ToggleButton("SR");
        sr.setTooltip(new Tooltip("SR"));
        sr.setPrefWidth(40);
        sr.setPrefHeight(40);
        sr.setOnAction(e-> {
            if(sr.isSelected())
            System.out.println(sr.getText()  + " ON");
            else
            System.out.println(sr.getText() + " OFF");
        });


        ToggleButton mh = new ToggleButton("MH");
        mh.setTooltip(new Tooltip("MH"));
        mh.setPrefWidth(40);
        mh.setPrefHeight(40);
        mh.setOnAction(e-> {
            if(mh.isSelected())
            System.out.println(mh.getText()  + " ON");
            else
            System.out.println(mh.getText() + " OFF");
        });

        ToggleButton ssfha = new ToggleButton("SSFHA");
        ssfha.setTooltip(new Tooltip("SSFHA"));
        ssfha.setPrefWidth(40);
        ssfha.setPrefHeight(40);
        ssfha.setOnAction(e-> {
            if(ssfha.isSelected())
            System.out.println(ssfha.getText()  + " ON");
            else
            System.out.println(ssfha.getText() + " OFF");
        });

        sr.setToggleGroup(group);
        mh.setToggleGroup(group);
        ssfha.setToggleGroup(group);

        //Menu
        VBox level = new VBox();
        level.getStyleClass().addAll("vbox");
        level.getChildren().addAll(sr, mh, ssfha);

        // add level to the center of this pane:

        setCenter(level);

    }
}