0

I have no exceptions/errors, just no result from trying to activate the button. When I click the button nothing happens, so, I think it's either some kind of hiccup in button mouse clicking method triggering or issues with doing the method that is doing int parsing. I have a class that is responsible for doing the task

"Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2. Beware: r must be without duplicates."

And it worked fine with the console, but Now I am switching the console to a JavaFX scene builder GUI. And I have no idea why I can't make this code work. Here is a code from my Array class.

public class Array_Shenanigans {
    public int size;
    SampleController cont = new SampleController();
    public String[] a1, a2, r;

    public int first_array_size() {
        try {
            size = Integer.parseInt(cont.input.getText());
        } catch (NumberFormatException e) {
            cont.labelOutput.setText("Please, input a proper size of the first array");
            return size = 0;
        }
        return size;
    }

    public void first_array_input() {
        a1 = new String[size];
        for (int i = 0; i != size; i++) {
            cont.labelOutput.setText("Input the array element number " + (i + 1));
            a1[i] = cont.input.getText();
        }
    }

    public int second_array_size() {
        try {
            size = Integer.parseInt(cont.input.getText());
        } catch (NumberFormatException e) {
            cont.labelOutput.setText("Please, input a proper size of the first array");
            return size = 0;
        }
        return size;
    }

    public void second_array_input() {
        a2 = new String[size];
        for (int i = 0; i != size; i++) {
            cont.labelOutput.setText("Input the array element number " + (i + 1));
            a2[i] = cont.input.getText();
        }
    }

    public void uber_array_creation() {
        ArrayList r1 = new ArrayList();
        for (int i = 0; i != a1.length; i++) {
            for (int j = 0; j != a2.length; j++) {
                if (a2[j].contains(a1[i])) {
                    r1.add(a1[i]);
                }
            }
        }
        Set<String> set = new HashSet<>(r1);
        r1.clear();
        r1.addAll(set);
        r = (String[]) r1.toArray(new String[r1.size()]);
    }

    public void uber_array_sort() {
        Arrays.sort(r);
    }


    public void uber_array_output() {
        String s = "";
        for (int i = 0; i < r.length; i++) {
            s = r[i] + " ";
            cont.labelOutput.setText(s);
        }
    }
}

My fxml file looke like that

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
            prefWidth="700.0" style="-fx-background-color: #2E3348;" xmlns="http://javafx.com/javafx/8"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SampleController">
    <children>
        <AnchorPane layoutY="87.0" prefHeight="313.0" prefWidth="700.0" style="-fx-background-color: #fafafa;">
            <children>
                <Button fx:id="countButton" layoutX="579.0" layoutY="125.0" mnemonicParsing="false"
                        onAction="#countButtonAction" prefHeight="25.0" prefWidth="74.0" text="Count"/>
                <Button layoutX="579.0" layoutY="157.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="74.0"
                        text="Save"/>
                <Button layoutX="579.0" layoutY="190.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="74.0"
                        text="Load"/>
                <TextField fx:id="input" alignment="CENTER_RIGHT" layoutX="526.0" layoutY="77.0"
                           promptText="Input goes here"/>
                <ComboBox fx:id="cmbB" layoutX="14.0" layoutY="52.0" onAction="#comboChanged" prefWidth="150.0"
                          promptText="Choose the task "/>
                <Label fx:id="taskLabel" layoutX="14.0" layoutY="85.0" prefHeight="130.0" prefWidth="236.0"/>
                <Label fx:id="labelOutput" layoutX="420.0" layoutY="36.0" prefHeight="33.0" prefWidth="255.0"/>
            </children>
        </AnchorPane>
        <Label layoutX="245.0" layoutY="14.0" prefHeight="43.0" prefWidth="334.0" text="Proverochka" textFill="WHITE">
            <font>
                <Font name="Comic Sans MS" size="39.0"/>
            </font>
        </Label>
    </children>
</AnchorPane>

And the Controller class is here, methods from Array class are getting information from TextField "input" via parsing String to int instead of regular input through the console and puts messages in Label "labelOutput" instead of system.out.Println()

public class SampleController implements Initializable, EventHandler<ActionEvent> {

    public Label taskLabel = new Label();
    public Label labelOutput = new Label();
    public TextField input = new TextField();
    public Button countButton = new Button();

    public void countButtonAction(ActionEvent event) {
        Array_Shenanigans array = new Array_Shenanigans();
        if ((labelOutput.getText() == "Input the size of the first array") || (labelOutput.getText() == "Please, input a proper size of the first array")) {
            array.first_array_size();
            if (array.size == 0)
                return;
            array.first_array_input();
            labelOutput.setText("Input the size of the second array");
        }

        if (labelOutput.getText() == "Input the size of the second array") {
            array.second_array_size();
            if (array.size == 0)
                return;
            array.second_array_input();
            array.uber_array_creation();
            array.uber_array_sort();
            array.uber_array_output();
        }
    }

    public ComboBox<String> cmbB;
    ObservableList<String> list = FXCollections.observableArrayList("Task 1 Arrays", "Task 2 Exp. numbers");

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        cmbB.setItems(list);
    }

    public void comboChanged(ActionEvent event) {
        if (cmbB.getValue() == "Task 1 Arrays") {
            taskLabel.setText("Given two arrays of strings a1 and a2" + "\n" + "return a sorted array r in lexicographical" + "\n" + "order of the strings of a1 which are" + "\n" + "substrings of strings of a2." + "\n" + "\n" + "Beware: r must be without duplicates.");
            labelOutput.setText("Input the size of the first array");
        }
        if (cmbB.getValue() == "Task 2 Exp. numbers") {
            taskLabel.setText("Write Number in Expanded Form. You will" + "\n" + "be given a number and you will need to" + "\n" + "return it as a string inExpanded Form. " + "\n" + "\n" + "NOTE: All numbers will be whole numbers" + "\n" + "greater than 0.");
            labelOutput.setText("Input the number to expand");
        }
    }

    @Override
    public void handle(ActionEvent event) {

    }   
}

All imports should be fine, didn't include them to save space. Main Class is here.

public class Main extends Application {
    @Override


    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));
        primaryStage.setTitle("Test task");
        primaryStage.setScene(new Scene(root, 700, 400));
        primaryStage.show();
        primaryStage.setMaxHeight(400);
        primaryStage.setMaxWidth(700);
        primaryStage.setMinHeight(400);
        primaryStage.setMinWidth(700);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65
Jermog
  • 67
  • 7
  • Please fix your formatting and remove the empty methods. – airsquared Mar 12 '19 at 23:51
  • What do you mean by "Formatting?" If you are talking about cntrl+K my code I already have done that. – Jermog Mar 12 '19 at 23:52
  • The indentation, the location of brackets(not consistent), and the empty methods make it hard to read. What IDE are you using? – airsquared Mar 12 '19 at 23:54
  • Intellij Idea. I did the formatting hotkey for all classes just in case and got rid of methods that are empty for now. – Jermog Mar 12 '19 at 23:58
  • Ctrl-K does not reformat code. See [here](https://www.jetbrains.com/help/idea/using-code-editor.html) for keyboard shortcuts in IntelliJ IDEA. – airsquared Mar 13 '19 at 00:02
  • Yeah, I did "Ctrl+Alt+L" in the Intellij Idea and then "Cntrl+K" here the copypasted code on stack overflow website because it's what I am asked to do before posting it. – Jermog Mar 13 '19 at 00:08
  • your not injecting your button you need to add the @FXML annotation – Mohammed Housseyn Taleb Mar 13 '19 at 01:39

1 Answers1

0

your controller skelton should be

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class SampleController {

    @FXML
    private Button countButton;

    @FXML
    private TextField input;

    @FXML
    private ComboBox<String> cmbB;

    @FXML
    private Label taskLabel;

    @FXML
    private Label labelOutput;

    ObservableList<String> list = FXCollections.observableArrayList("Task 1 Arrays", "Task 2 Exp. numbers");



@Override
public void initialize(URL location, ResourceBundle resources) {
    cmbB.setItems(list);
}

@FXML
public void comboChanged(ActionEvent event) {
    if (cmbB.getValue() == "Task 1 Arrays") {
        taskLabel.setText("Given two arrays of strings a1 and a2" + "\n" + "return a sorted array r in lexicographical" + "\n" + "order of the strings of a1 which are" + "\n" + "substrings of strings of a2." + "\n" + "\n" + "Beware: r must be without duplicates.");
        labelOutput.setText("Input the size of the first array");
    }
    if (cmbB.getValue() == "Task 2 Exp. numbers") {
        taskLabel.setText("Write Number in Expanded Form. You will" + "\n" + "be given a number and you will need to" + "\n" + "return it as a string inExpanded Form. " + "\n" + "\n" + "NOTE: All numbers will be whole numbers" + "\n" + "greater than 0.");
        labelOutput.setText("Input the number to expand");
    }
}    
    @FXML
public void countButtonAction(ActionEvent event) {
    Array_Shenanigans array = new Array_Shenanigans();
    if ((labelOutput.getText() == "Input the size of the first array") || (labelOutput.getText() == "Please, input a proper size of the first array")) {
        array.first_array_size();
        if (array.size == 0)
            return;
        array.first_array_input();
        labelOutput.setText("Input the size of the second array");
    }

    if (labelOutput.getText() == "Input the size of the second array") {
        array.second_array_size();
        if (array.size == 0)
            return;
        array.second_array_input();
        array.uber_array_creation();
        array.uber_array_sort();
        array.uber_array_output();
    }

}

}

your fxml interface looks like enter image description here

to understand what is @FXML annotation read this

  • 2
    When a member is `public`, it [doesn't need](https://openjfx.io/javadoc/11/javafx.fxml/javafx/fxml/doc-files/introduction_to_fxml.html#fxml_annotation) the `@FXML` annotation. – Slaw Mar 13 '19 at 02:43
  • Thanks, I knew there was somehting about @FXML that I missed. Gonna try this out and implement everything properly. – Jermog Mar 13 '19 at 05:59