1

I've been trying to create a simple table made up of values a user can input. However, it seems I'm missing something from my code, despite it compiling without errors. I've been reviewing this code for about two hours now and I still can't see anything wrong with it. Is anyone here able to help?

The button also does not work.

The main4 class contains the main method. Main3 is a constructor.

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 *
 * @author SHAWN
 */
public class Main4 extends Application {

    @Override
    public void start(Stage primaryStage){
        primaryStage.setTitle("999 Emergency Operator System");
        primaryStage.setWidth(450);
        primaryStage.setHeight(550);

        TableView<Main3> table = new TableView<>();
        ObservableList<Main3> data = FXCollections.observableArrayList(
        new Main3("-","-","-"),
        new Main3("-","-","-"));
        table.setItems(data);
        table.setEditable(true);
        Label label = new Label("Operator Reports Pannel");
        label.setFont(new Font("Arial", 20));

        //Creation of Columns here!
        TableColumn firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(200);
            firstNameCol.setCellValueFactory(new PropertyValueFactory<>("fName"));
    TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(25);
            lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lName"));
    TableColumn emailCol = new TableColumn("Email)");
        emailCol.setMinWidth(25);
            emailCol.setCellValueFactory(new PropertyValueFactory<>("mail"));

        //Creation of TextFields
        TextField firstName, lastName, mail;
        firstName = new TextField();
        firstName.setPromptText("First Name");
        firstName.setMinWidth(25);

        lastName = new TextField();
        lastName.setPromptText("Last Name");
        lastName.setMinWidth(25);

        //Time TextFields here
        mail = new TextField();
        mail.setPromptText("Email Address");
        mail.setMinWidth(25);


        Button AddButton = new Button("Add");
        AddButton.setOnAction(e ->{
            data.add(
            new Main3(
            firstName.getText(),
                    lastName.getText(),
                    mail.getText())

            );
        });
        firstName.clear();
        lastName.clear();
        mail.clear();


        HBox hbox1 = new HBox(5);
        hbox1.getChildren().addAll(firstName, lastName, mail, AddButton);

        VBox vbox1 =new VBox(5);
        vbox1.getChildren().addAll(label, table, hbox1);
        vbox1.setPadding(new Insets(10, 0, 0, 10));
        Scene scene = new Scene(new Group());
        ((Group)scene.getRoot()).getChildren().addAll(vbox1);

        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

}

I have a feeling the mistake is in the Main3 class, but I can't find it.

import javafx.beans.property.SimpleStringProperty;

public class Main3 {
    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty email;

    public Main3( String fName, String lName, String mail){

        this.firstName = new SimpleStringProperty(fName);
        this.lastName = new SimpleStringProperty(lName);
        this.email = new SimpleStringProperty(mail);


}
    public String getFirstName(){
        return firstName.get();
    }
    public String getLastName(){
        return lastName.get();
    }
    public String getEmail(){
        return email.get();
    }
    public void setFirstName(String fName){
        firstName.set(fName);
    }
    public void setLastName(String LName){
        lastName.set(LName);
    }
    public void setEmailName(String mail){
        email.set(mail);
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shawn
  • 19
  • 3
  • 2
    You created three (raw) TableColumns… and forgot to add them to [the TableView’s column list](https://openjfx.io/javadoc/11/javafx.controls/javafx/scene/control/TableView.html#getColumns%28%29). – VGR Dec 18 '18 at 23:10
  • 2
    Also https://stackoverflow.com/questions/18971109/javafx-tableview-not-showing-data-in-all-columns – fabian Dec 18 '18 at 23:21
  • I don't understand. I'm new to java; could you possibly explain further? – Shawn Dec 18 '18 at 23:32
  • if you don't understand the comments, you'll need to read some very basic tutorials: starting with plain java and then going on to javafx :) – kleopatra Dec 20 '18 at 16:37

0 Answers0