-1

Building one of my first "major" GUI programs for my class but currently stuck at how to make the first window close when arriving to the "show iteration" section. First window always stays open and shows the original data, but I want the show iteration button to show the iterations with a constant original data on top as the first line of data. My code's kind of all over the place since its my first serious JAVAFX program I'm building hope its understandable! (I also keep getting a 0 in the output for some reason in the beginning.)Picture of Output

package com.example.assignment4;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
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.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

import java.util.Arrays;
import java.util.Random;


public class Sorting extends Application {
    // Variables
    private TextField minText, maxText;
    private static int defaultSize = 10;
    private static int[] arr = new int[defaultSize];
    private static int count = 0;
    private static Button sortingButton = new Button("Show Iteration");
    private Stage stage = new Stage();
    private static String out = Arrays.toString(arr);
    private static Label totalLabel;

    private static int[] randomArr;
    Scene scene;

    @Override
    public void start(Stage stage) {

        // Creating all input Labels and TextField inputs
        Label sortingTitleLabel = new Label("Sorting");
        sortingTitleLabel.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 50));
        sortingTitleLabel.setTextFill(Color.ALICEBLUE.darker());

        // Create Bubble Sort button option
        Button bubbleSortButton = new Button("Bubble Sort");
        bubbleSortButton.setFont(Font.font("Verdana", 30));
        bubbleSortButton.setMaxSize(1000, 1000);
        // Event handler for Bubble Sort button lambda style
        bubbleSortButton.setOnAction(actionEvent -> {
            stage.close();

            Label bubbleSortLabel = new Label("Bubble Sort");
            bubbleSortLabel.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 50));
            bubbleSortLabel.setTextFill(Color.ALICEBLUE.darker());

            // Create two button options
            // First option will be to Generate Random #'s
            Button generateButton = new Button("Generate Random #'s");
            generateButton.setFont(Font.font("Verdana", 30));
            generateButton.setMaxSize(1000, 1000);

            // Event handler for Generate Random #'s button lambda style
            generateButton.setOnAction(actionEvent1 -> {
                stage.close();

                //Label bubbleSortLabel = new Label("Bubble Sort");
                bubbleSortLabel.setText("Bubble Sort");
                bubbleSortLabel.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 50));
                bubbleSortLabel.setTextFill(Color.ALICEBLUE.darker());

                // Create Label and TextField variables for Min/Max inputs
                Label minLabel = new Label("Min. Value: ");
                minLabel.setFont(Font.font("Verdana", FontWeight.BOLD, 15));
                minText = new TextField();
                Label maxLabel = new Label("Max. Value: ");
                maxLabel.setFont(Font.font("Verdana", FontWeight.BOLD, 15));
                maxText = new TextField();

                // Show data button to show full array
                Button showDataButton = new Button("Show Data");
                showDataButton.setFont(Font.font("Verdana", 20));
                showDataButton.setMaxSize(200, 200);

                showDataButton.setOnAction(actionEvent2 -> {
                    stage.close();
                    // Create Data label for output
                    arr = generateRand();
                    Label dataLabel = new Label("Data: ");
                    dataLabel.setFont(Font.font("Verdana", FontWeight.BLACK, 25));
                    Label dataArrLabel = new Label(Arrays.toString(randomArr));
                    dataArrLabel.setFont(Font.font("Verdana", FontWeight.SEMI_BOLD, 25));

                    // Create Sorting button
                    //Button sortingButton = new Button("Show Iteration");
                    sortingButton.setOnAction(new showIteration());

                    HBox dataHbox = new HBox(10, dataLabel, dataArrLabel);
                    dataHbox.setAlignment(Pos.CENTER);
                    VBox showDataVbox = new VBox(10, bubbleSortLabel, dataHbox, sortingButton);
                    showDataVbox.setAlignment(Pos.CENTER);
                    showDataVbox.setPadding(new Insets(50, 100, 50, 100));
                    stage.setScene(new Scene(showDataVbox));
                    stage.show();


                });

                HBox minHbox = new HBox(10, minLabel, minText);
                HBox maxHbox = new HBox(10, maxLabel, maxText);
                VBox minMaxVbox = new VBox(20, minHbox, maxHbox,  showDataButton);
                minMaxVbox.setAlignment(Pos.CENTER);
                minMaxVbox.setPadding(new Insets(50, 100, 50, 100));
                stage.setScene(new Scene(minMaxVbox));
                stage.show();
            });

            // Second option will be to ask for User Input
            Button userInputButton = new Button("User Input");
            userInputButton.setFont(Font.font("Verdana", 30));
            userInputButton.setMaxSize(1000, 1000);

            VBox titleVbox = new VBox(bubbleSortLabel);
            titleVbox.setAlignment(Pos.TOP_CENTER);
            VBox titleButtonVbox = new VBox(30, generateButton, userInputButton);
            VBox titlePageVBox = new VBox(80, titleVbox, titleButtonVbox);
            titlePageVBox.setAlignment(Pos.CENTER);
            titlePageVBox.setPadding(new Insets(50, 100, 50, 100));
            Scene scene = new Scene(titlePageVBox);
            stage.setScene(scene);

            stage.show();


        });
        Button mergeSortButton = new Button("Merge Sort");
        mergeSortButton.setFont(Font.font("Verdana", 30));
        mergeSortButton.setMaxSize(1000, 1000);
        // Event handler for Merge Sort button lambda style
        mergeSortButton.setOnAction(actionEvent -> {
            stage.close();
            Stage secondStage = new Stage();
            Label mergeSortLabel = new Label("Merge Sort");
            mergeSortLabel.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 50));
            mergeSortLabel.setTextFill(Color.ALICEBLUE.darker());

            // Create two button options
            // First option will be to Generate Random #'s
            Button generateButton = new Button("Generate Random #'s");
            generateButton.setFont(Font.font("Verdana", 30));
            generateButton.setMaxSize(1000, 1000);

            // Second option will be to ask for User Input
            Button userInputButton = new Button("User Input");
            userInputButton.setFont(Font.font("Verdana", 30));
            userInputButton.setMaxSize(1000, 1000);

            VBox titleVbox = new VBox(mergeSortLabel);
            titleVbox.setAlignment(Pos.TOP_CENTER);
            VBox titleButtonVbox = new VBox(30, generateButton, userInputButton);
            VBox titlePageVBox = new VBox(80, titleVbox, titleButtonVbox);
            titlePageVBox.setAlignment(Pos.CENTER);
            titlePageVBox.setPadding(new Insets(50, 100, 50, 100));
            Scene scene = new Scene(titlePageVBox);
            secondStage.setScene(scene);

            secondStage.show();


        });



        VBox titleVbox = new VBox(sortingTitleLabel);
        titleVbox.setAlignment(Pos.TOP_CENTER);
        VBox titleButtonVbox = new VBox(30, bubbleSortButton, mergeSortButton);
        VBox titlePageVBox = new VBox(80, titleVbox, titleButtonVbox);
        titlePageVBox.setAlignment(Pos.CENTER);
        titlePageVBox.setPadding(new Insets(50, 100, 50, 100));
        Scene scene = new Scene(titlePageVBox);
        stage.setScene(scene);
        stage.show();

    }

    class showIteration implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent event) {
            stage.close();
            stage.close();
            String newArray = bubbleSort();
            Node node = (Node) event.getSource();
            Label bubbleSortLabel = new Label("Bubble Sort");
            bubbleSortLabel.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 50));
            bubbleSortLabel.setTextFill(Color.ALICEBLUE.darker());
            Label dataLabel = new Label("Data: \n");
            dataLabel.setFont(Font.font("Verdana", FontWeight.BLACK, 25));
            Label dataArrLabel = new Label(Arrays.toString(randomArr));
            dataArrLabel.setFont(Font.font("Verdana", FontWeight.SEMI_BOLD, 25));
            Label iteratedArrLabel = new Label(newArray);
            //iteratedArrLabel.setText(bubbleSort());
            iteratedArrLabel.setFont(Font.font("Verdana", FontWeight.SEMI_BOLD, 25));
            HBox dataHBox = new HBox(10, dataLabel, dataArrLabel);
            VBox iteratedVBox = new VBox(10, bubbleSortLabel, dataHBox, iteratedArrLabel, sortingButton);
            iteratedVBox.setAlignment(Pos.CENTER);
            iteratedVBox.setPadding(new Insets(50, 100, 50, 100));
            stage.setScene(new Scene(iteratedVBox));
            stage = (Stage) node.getScene().getWindow();
            stage.show();


        }
    }

    public String bubbleSort() {
        stage.close();
        int n = arr.length;
        int temp = 0;
        for(int i=0; i < n; i++){
            for(int j=1; j < (n-i); j++){
                if(arr[j-1] > arr[j]){
                    //swap elements
                    temp = Sorting.arr[j-1];
                    arr[j-1] = arr[j];
                    arr[j] = temp;
                    break;
                }
            }
        }
        
        out = out + "\n" + Arrays.toString(arr);
        return out;
    }

    public int[] generateRand() {
        stage.close();
        // Create the random number array within the min and max provided
        int minValue = Integer.parseInt(minText.getText());
        int maxValue = Integer.parseInt(maxText.getText());
        Random randy = new Random();

        for (int i = 0; i < defaultSize; i++) {
            arr[i] = (randy.nextInt(maxValue + 1 - minValue) + minValue);
        }
        randomArr = arr;
        return arr;
    }

    public static void main(String[] args) {
        launch();
    }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
Yayo
  • 15
  • 1
  • 7
  • 1
    I looked at your code. I am not sure why you are doing things like `stage.close()` in the methods `bubbleSort` and `generateRand`. I also do not know what's happening with `Sorting.arr[j-1]` in the method `bubbleSort`. What is `Sorting` there? – SedJ601 Mar 21 '22 at 04:11
  • 1
    See if [this](https://stackoverflow.com/questions/53573747/dynamic-bar-chart-in-java-fx/53579013#53579013) can help you. – SedJ601 Mar 21 '22 at 04:12
  • ignore the Sorting.arr its basically "arr" I forgot to change it. I had the stage.close to try to attempt to close the window before the windows that show the iterations. – Yayo Mar 21 '22 at 04:14
  • 1
    [mcve] please.. nothing unrelated (like sorting f.i.) just ui with a button to open a new stage – kleopatra Mar 21 '22 at 09:25
  • 2
    unrelated: don't use static scope .. – kleopatra Mar 21 '22 at 11:30

1 Answers1

-1

I have absolutely no clue where you are creating the second window, but i found a simple hotfix for your problem:

sortingButton.setOnAction(event -> {
    stage.close();
    new ShowIteration().handle(event);
});

Idk if this woulnd't lead to any other problems in future & i'm not sure if there is a cleaner solution, but it works.

Edit: you wrote showIteration lowecase; I changed that to upper case because you are violating naming conventions

Stevie
  • 308
  • 2
  • 11