2

I am writing program that moves digits from TextField to TextArea for sorting or shuffling or reversing them. All entered digits have to be save in ArrayList.

At first I create javafx stage with buttons labels and etc. Then I added listeners for buttons and for moving text from TextField to TextArea. I run succefully my program couple times before I assign to store digits in ArrayList. Then I got exception message

Exception in Application constructor
Exception in thread "main" java.lang.NoSuchMethodException: StoreNumbers.main([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1778)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:119)

I have search on web and on stackoverflow but didn't find something that help me to understand what I am doing wrong. Below my code. Thank you for help in advance for any advices.

 import javafx.application.Application;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
 import javafx.scene.control.*;
 import javafx.scene.input.KeyCode;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.FlowPane;
 import javafx.scene.layout.HBox;
 import javafx.scene.layout.Pane;
 import javafx.stage.Stage;
 import java.util.*;

 public class StoreNumbers extends Application {
    private TextField textField = new TextField();
    private TextArea textArea = new TextArea();
    StoreNumberPane snPane = new StoreNumberPane();
    List<String> arrayList = new ArrayList<>();
    public void start(Stage primaryStage) {

    Button btSort = new Button("Sort");
    Button btShuffle = new Button("Shuffle");
    Button btReverse = new Button("Reverse");
    HBox hBox = new HBox(10);
    hBox.getChildren().addAll(btSort, btShuffle, btReverse);
    hBox.setAlignment(Pos.CENTER);

    btSort.setOnAction(e -> snPane.sort());
    btShuffle.setOnAction(e -> snPane.shuffle());
    btReverse.setOnAction(e -> snPane.reverse());

    Label label = new Label("Enter a number: ");

    textField.setPromptText("Enter integer");
    FlowPane flowPane = new FlowPane();

    flowPane.getChildren().addAll(label, textField);

    ScrollPane scrollPane = new ScrollPane(textArea);

    BorderPane pane = new BorderPane();
    pane.setCenter(textArea);
    pane.setBottom(hBox);
    pane.setTop(flowPane);

    pane.setOnKeyPressed(e -> {
        if (e.getCode() == KeyCode.ENTER) {
            obtainData();
            textField.clear();
        }
    });

    Scene scene = new Scene(pane, 400, 400);
    primaryStage.setTitle("Exercise20_20");
    primaryStage.setScene(scene);
    primaryStage.show();

    snPane.requestFocus();
}

    public void displayArray(){

    }

    private void obtainData(){

        String userInput = textField.getText();
        if(userInput != null)

        arrayList.add(userInput);

        textArea.setText(String.format("%s", arrayList));
    }



}

class StoreNumberPane extends Pane{

StoreNumbers storeNumbers = new StoreNumbers();




public void sort(){
    Collections.sort(storeNumbers.arrayList);

}

public void shuffle(){
    Collections.shuffle(storeNumbers.arrayList);
}

public void reverse(){
    Collections.reverse(storeNumbers.arrayList);
}
}
Pramod Karandikar
  • 5,289
  • 7
  • 43
  • 68
rob111
  • 1,249
  • 1
  • 13
  • 18

2 Answers2

0

The problem with your application is you are trying to create a new instance of the application class again from the StoreNumberPane, even before the old instance has finished instantiating itself.

There should be only one application class instance int eh whole application. So creating a new instance won't help. JavaFX won't throw an error for it, but it is not the instance you are looking for

Now, you just need an arraylist in the StoreNumberPane, you have many options to get it.

  • Make the ArrayList as static which can now be easily accessed from anywhere
  • Create a method which returns the current instance of the class and use it to access the list
  • The last and the best way would be to pass the lsit you want to sort, shuffle or reverse

Your call becomes,

btSort.setOnAction(e -> snPane.sort(arrayList));

Your method declaration becomes,

public void sort(List list) {
    Collections.sort(list);
}

Though I am still not sure, why are you using theStoreNumberPanewhich extendsPane. It doesn't do anything that needs Pane to be extended.

A complete example which sorts string:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class StoreNumbers extends Application {
    private TextField textField = new TextField();
    private TextArea textArea = new TextArea();
    StoreNumberPane snPane = new StoreNumberPane();
    public List<String> arrayList = new ArrayList<>();

    public void start(Stage primaryStage) {

        Button btSort = new Button("Sort");
        Button btShuffle = new Button("Shuffle");
        Button btReverse = new Button("Reverse");
        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(btSort, btShuffle, btReverse);
        hBox.setAlignment(Pos.CENTER);

        btSort.setOnAction(e -> { snPane.sort(arrayList); refreshData(); });
        btShuffle.setOnAction(e -> { snPane.shuffle(arrayList); refreshData(); });
        btReverse.setOnAction(e -> { snPane.reverse(arrayList); refreshData(); });

        Label label = new Label("Enter a number: ");

        textField.setPromptText("Enter String");
        textArea.setEditable(false);
        FlowPane flowPane = new FlowPane();

        flowPane.getChildren().addAll(label, textField);

        ScrollPane scrollPane = new ScrollPane(textArea);

        BorderPane pane = new BorderPane();
        pane.setCenter(textArea);
        pane.setBottom(hBox);
        pane.setTop(flowPane);

        pane.setOnKeyPressed(e -> {
            if (e.getCode() == KeyCode.ENTER) {
                obtainData();
                textField.clear();
            }
        });

        Scene scene = new Scene(pane, 400, 400);
        primaryStage.setTitle("Exercise20_20");
        primaryStage.setScene(scene);
        primaryStage.show();

        snPane.requestFocus();
    }

    public void displayArray() {

    }

    private void obtainData() {
        String userInput = textField.getText();
        if (userInput != null)
            arrayList.add(userInput);
        textArea.setText(String.format("%s", arrayList));
    }

    private void refreshData() {
        textArea.clear();
        textArea.setText(String.format("%s", arrayList));
    }
    public static void main(String[] args) {
        launch(args);
    }

}

class StoreNumberPane extends Pane {

    public void sort(List<String> list) {
        Collections.sort(list);
    }

    public void shuffle(List<String> list) {
        Collections.shuffle(list);
    }

    public void reverse(List<String> list) {
        Collections.reverse(list);
    }

}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • First of all thank you very much you explanation. But I didn't get what you mean by this "The problem with your application is you are trying to create a new instance of the application class again from the StoreNumberPane, even before the old instance has finished instantiating itself." Could you clarify? – rob111 Feb 11 '15 at 05:02
  • I am trying to show what actually happens in your application `launch` -> StoreNumbers -> StoreNumberPane -> StoreNumbers -> StoreNumberPane ... and this goes on and on. – ItachiUchiha Feb 11 '15 at 06:33
  • I got it. Thank you to answer on my question. – rob111 Feb 12 '15 at 01:20
-2

Your extending from class application. By doing so you always need the main function to be declared.

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

Have a look at this simple oracle example...helloworld

Inge
  • 427
  • 7
  • 20