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);
}
}