0

I'am currently working on a project where I'am trying to implement a TableView that takes in user input (Name, Rating, Restaurant Name). Below I have provided my code that is already in my Controller class. The problem that I'm having is that whenever I type anything in the textfields and click the "submit" button it does absolutely nothing. What could possibly be the problem?

Here is the beginning and end of my code, I did not include the rest because the Login method is the only one giving me problems when I try to initialize.

public class MainController{


@FXML
private Label lblStatus;
@FXML
private TextField txtUsername;
@FXML
private TextField txtPassword;



/*This method allows user login and when login is successful then user is taken to the main screen.*/     


public void Login (ActionEvent event) throws Exception {
    if (txtUsername.getText().equals("user") && txtPassword.getText().equals("pass")) {
        lblStatus.setText("Login Success");
        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("/application/RestScene.fxml"));
        Scene scene = new Scene(root,400,400);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setTitle("Restaurant Advisor");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    else
    {
        lblStatus.setText("Login Failed");
    }
}



public TableView<Table> tableview;
public TableColumn<Table, String> colName;
public TableColumn<Table, Integer> colRating;
public TableColumn<Table, String>colRestaurants;
public TextField txtFieldName;
public TextField txtFieldRating;
public TextField txtFieldRestaurant;

public void initialize(URL location, ResourceBundle resources) {
    colName.setCellValueFactory(new PropertyValueFactory<>("TableName"));
    colRating.setCellValueFactory(new PropertyValueFactory<>("TableRating"));
    colRestaurants.setCellValueFactory(new PropertyValueFactory<>("TableRestaurants"));
    tableview.setItems(observableList);
}

ObservableList<Table> observableList = FXCollections.observableArrayList(new Table ("Test", 1, "makiyaki"));


public void buttonSubmit(ActionEvent event) {

    Table table = new Table (txtFieldName.getText(), Integer.parseInt(txtFieldRating.getText()), txtFieldRestaurant.getText());
    tableview.getItems().add(table);
}

Here is the FXML code for the specific tableview

    <AnchorPane layoutX="596.0" layoutY="338.0" prefHeight="284.0" prefWidth="461.0">
         <children>
            <TableView fx:id="tableview" layoutX="-3.0" layoutY="-1.0" prefHeight="243.0" prefWidth="315.0">
              <columns>
                <TableColumn prefWidth="75.0" text="Name" />
                <TableColumn prefWidth="75.0" text="Rating" />
                  <TableColumn prefWidth="75.0" text="Restaurant" />
              </columns>
            </TableView>
            <TextField fx:id="txtFieldName" layoutX="39.0" layoutY="253.0" prefHeight="12.0" prefWidth="83.0" promptText="Name" />
            <TextField fx:id="txtFieldRating" layoutX="173.0" layoutY="249.0" prefHeight="12.0" prefWidth="83.0" promptText="Rating" />
            <TextField fx:id="txtFieldRestaurant" layoutX="317.0" layoutY="239.0" prefHeight="12.0" prefWidth="83.0" promptText="Restaurant" />
            <Button layoutX="341.0" layoutY="58.0" mnemonicParsing="false" onAction="#buttonSubmit" text="Submit" />
         </children>
      </AnchorPane>
   </children>
</AnchorPane>
Blythe S.
  • 11
  • 5

2 Answers2

1
  • You haven't inserted fx:id into your table columns in the fxml. Its gonna cause a NullPointerException in your initalize() as soon its run, from what you said that nothing happens when you click on submit, it means that the initialize() hasn't been called by the application in the first place.

  • Your initialize() needs to be a no-arg method for it to get called. See here or else if you want to do it your way i.e initialize(URL, ResourceBundle) make your controller implement the aforementioned Initializable interface

  • go through this question once your're done
Ryotsu
  • 786
  • 6
  • 16
  • Since my controller class includes other code that does not need to be initialized, I'm not able to do this. It gives me errors. – Blythe S. Dec 01 '18 at 16:05
  • I don't understand. Just replace `public class MainController` with `public class MainController implements Initializable` and import the `Initializable` interface. – Ryotsu Dec 02 '18 at 02:18
  • When I do that i get errors, whenever I implements Initializable. i've posted my updated code above without the initializable interface because it gives me errors with it implemented – Blythe S. Dec 02 '18 at 22:25
  • Then just remove the parameters in the `initialize()` – Ryotsu Dec 03 '18 at 02:12
-1

You're missing @FXML tag on your buttonSubmit method

Gnas
  • 698
  • 1
  • 6
  • 14
  • I added @FXML but it didn't change anything – Blythe S. Dec 01 '18 at 01:23
  • 1
    This shouldn't matter. `public` members are accessible to `FXMLLoader` by default. Only non-public members need to be annotated. – fabian Dec 01 '18 at 01:24
  • Does it matter at all that I placed the tableview in an anchorpane within the main anchorpane? – Blythe S. Dec 01 '18 at 01:28
  • @BlytheS. No this does not matter. In fact you do not need to create a scene using fxml; you could also e.g. create a `ArrayList` of `String`s. Injection/handlers should work regardless of the object structure created based on the fxml. – fabian Dec 01 '18 at 01:32