So I'm coding for an assessment and came across this issue where my table view doesn't show some columns when data is entered.
The table is a graphical representation of the results of a race. The proper way of entering data into the table would be when I read a file called "winner.txt". But I haven't figured that part of the code yet so for now I manually entered the data.
The code that redirects from my question deals with 'String' data types in the tableView while my tableView deals with 'SimpleStringProperty'. So if someone can help me out it would be much appreciated.
Ive attached the code for my table view below along with a screenshot of the output.
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class UserInterface extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage){
AnchorPane anchorPane = new AnchorPane();
SplitPane splitPane = new SplitPane();
splitPane.setOrientation(Orientation.VERTICAL);
AnchorPane anchorPane1 = new AnchorPane();
AnchorPane anchorPane2 = new AnchorPane();
//for Class A
Label venue1 = new Label("Class A");
TableView<Class_A> tableView1 = new TableView<>();
TableColumn<Class_A,String> tableColumnVenue1 = new TableColumn<>("Place");
TableColumn<Class_A,String> tableColumnVenue1_ClassA = new TableColumn<>("Venue 01");
TableColumn<Class_A,String> tableColumnAVenue1_Name = new TableColumn<>("Name");
TableColumn<Class_A,Double> tableColumnAVenue1_Time = new TableColumn<>("Time");
TableColumn<Class_A,Integer> tableColumnAVenue1_LapNo = new TableColumn<>("Lap No.");
TableColumn<Class_A,String> tableColumnBVenue1_ClassB = new TableColumn<>("Venue 02");
TableColumn<Class_A,String> tableColumnBVenue1_Name = new TableColumn<>("Name");
TableColumn<Class_A,Double> tableColumnBVenue1_Time = new TableColumn<>("Time");
TableColumn<Class_A,Integer> tableColumnBVenue1_LapNo = new TableColumn<>("Lap No.");
tableColumnVenue1_ClassA.getColumns().add(tableColumnAVenue1_Name);
tableColumnVenue1_ClassA.getColumns().add(tableColumnAVenue1_Time);
tableColumnVenue1_ClassA.getColumns().add(tableColumnAVenue1_LapNo);
tableColumnBVenue1_ClassB.getColumns().add(tableColumnBVenue1_Name);
tableColumnBVenue1_ClassB.getColumns().add(tableColumnBVenue1_Time);
tableColumnBVenue1_ClassB.getColumns().add(tableColumnBVenue1_LapNo);
tableView1.getColumns().add(tableColumnVenue1);
tableView1.getColumns().add(tableColumnVenue1_ClassA);
tableView1.getColumns().add(tableColumnBVenue1_ClassB);
anchorPane1.getChildren().addAll(venue1,tableView1);
//for Class B
Label venue2 = new Label("Class B");
TableView<Class_B> tableView2 = new TableView<>();
TableColumn<Class_B,String> tableColumnVenue2 = new TableColumn<>("Place");
TableColumn<Class_B,String> tableColumnVenue2_ClassA = new TableColumn<>("Venue 01");
TableColumn<Class_B,String > tableColumnAVenue2_Name = new TableColumn<>("Name");
TableColumn<Class_B,Double> tableColumnAVenue2_Time = new TableColumn<>("Time");
TableColumn<Class_B,Integer> tableColumnAVenue2_LapNo = new TableColumn<>("Lap No.");
TableColumn<Class_B,String > tableColumnBVenue2_ClassB = new TableColumn<>("Venue 02");
TableColumn<Class_B,String > tableColumnBVenue2_Name = new TableColumn<>("Name");
TableColumn<Class_B,Double> tableColumnBVenue2_Time = new TableColumn<>("Time");
TableColumn<Class_B,Integer> tableColumnBVenue2_LapNo = new TableColumn<>("Lap No.");
tableColumnVenue2_ClassA.getColumns().add(tableColumnAVenue2_Name);
tableColumnVenue2_ClassA.getColumns().add(tableColumnAVenue2_Time);
tableColumnVenue2_ClassA.getColumns().add(tableColumnAVenue2_LapNo);
tableColumnBVenue2_ClassB.getColumns().add(tableColumnBVenue2_Name);
tableColumnBVenue2_ClassB.getColumns().add(tableColumnBVenue2_Time);
tableColumnBVenue2_ClassB.getColumns().add(tableColumnBVenue2_LapNo);
tableView2.getColumns().add(tableColumnVenue2);
tableView2.getColumns().add(tableColumnVenue2_ClassA);
tableView2.getColumns().add(tableColumnBVenue2_ClassB);
anchorPane2.getChildren().addAll(venue2,tableView2);
splitPane.getItems().addAll(anchorPane1,anchorPane2);
anchorPane.getChildren().addAll(splitPane);
//Adding data to the columns
//for class A
tableView1.getItems().add(new Class_A("1st","","","Alastair Heath",3.55,0,"Josh West ",2.70,0));
tableView1.getItems().add(new Class_A("2nd","","","Alastair Heath",3.55,0,"Josh West ",2.70,0));
tableView1.getItems().add(new Class_A("3rd","","","Alastair Heath",3.55,0,"Josh West ",2.70,0));
tableView1.getItems().add(new Class_A("Fastest Lap","","","Alastair Heath",3.55,0,"Josh West ",2.70,0));
//for Class B
tableView2.getItems().add(new Class_B("1st","","","Alastair Heath",3.55,0,"London West ",2.70,0));
tableView2.getItems().add(new Class_B("2nd","","","Alastair Heath",3.55,0,"Josh West ",2.70,0));
tableView2.getItems().add(new Class_B("3rd","","","Alastair Heath",3.55,0,"Josh West ",2.70,0));
tableView2.getItems().add(new Class_B("Fastest Lap","","","Alastair Heath",3.55,0,"Josh West ",2.70,0));
//For Class A
tableColumnVenue1.setCellValueFactory(new PropertyValueFactory<>("placeA"));
//Venue 01
//tableColumnVenue1_ClassA.setCellValueFactory(new PropertyValueFactory<>("venue1A"));
tableColumnAVenue1_Name.setCellValueFactory(new PropertyValueFactory<>("name1A"));
tableColumnAVenue1_Time.setCellValueFactory(new PropertyValueFactory<>("time1A"));
tableColumnAVenue1_LapNo.setCellValueFactory(new PropertyValueFactory<>("lapNo1A"));
//Venue 02
tableColumnBVenue2_Name.setCellValueFactory(new PropertyValueFactory<>("name2A"));
tableColumnBVenue2_Time.setCellValueFactory(new PropertyValueFactory<>("time2A"));
tableColumnBVenue2_LapNo.setCellValueFactory(new PropertyValueFactory<>("lapNo2A"));
//For Class B
tableColumnVenue2.setCellValueFactory(new PropertyValueFactory<>("placeB"));
//Venue 01
//tableColumnBVenue1_ClassB.setCellValueFactory(new PropertyValueFactory<>("venueB"));
tableColumnAVenue1_Name.setCellValueFactory(new PropertyValueFactory<>("name1B"));
tableColumnAVenue1_Time.setCellValueFactory(new PropertyValueFactory<>("time1B"));
tableColumnAVenue1_LapNo.setCellValueFactory(new PropertyValueFactory<>("lapNo1B"));
//Venue 02
tableColumnBVenue2_Name.setCellValueFactory(new PropertyValueFactory<>("name2B"));
tableColumnBVenue2_Time.setCellValueFactory(new PropertyValueFactory<>("time2B"));
tableColumnBVenue2_LapNo.setCellValueFactory(new PropertyValueFactory<>("lapNo2B"));
//CSS
anchorPane.setPrefSize(600,450);
splitPane.setPrefSize(600,450);
//top
anchorPane1.setPrefSize(600,225);
venue1.setPrefSize(600,30);
venue1.setTextFill(Color.web("#1a0dcd"));
venue1.setFont(Font.font("Arial", FontWeight.BOLD,20));
venue1.setTextAlignment(TextAlignment.CENTER);
tableColumnAVenue1_Name.setPrefWidth(100);
tableColumnBVenue1_Name.setPrefWidth(100);
tableView1.setPrefSize(600,185);
tableView1.setLayoutX(0);
tableView1.setLayoutY(25);
//bottom
anchorPane2.setPrefSize(600,225);
venue2.setPrefSize(600,30);
venue2.setTextFill(Color.web("#1a0dcd"));
venue2.setFont(Font.font("Arial", FontWeight.BOLD,20));
venue2.setTextAlignment(TextAlignment.CENTER);
tableColumnAVenue2_Name.setPrefWidth(100);
tableColumnBVenue2_Name.setPrefWidth(100);
tableView2.setPrefSize(600,185);
tableView2.setLayoutX(0);
tableView2.setLayoutY(25);
Scene scene = new Scene(anchorPane);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setTitle("FORMULA 01 - RACE RESULTS");
//Prevents resizing window
primaryStage.setResizable(false);
primaryStage.setMinWidth(600);
primaryStage.setMinHeight(450);
}
}
And here are the Classes relevant to the above code:
Class A:
/*
* Racers with
* Max Age : 17
* Max horsepower : 300
*/
public class Class_A {
//Instance variables
private String placeA;
private final String venue1A;
private final String venue2A;
private String name1A;
private double time1A;
private int lapNo1A;
private String name2A;
private double time2A;
private int lapNo2A;
public Class_A(String placeA, String venue1A, String venue2A, String name1A, double time1A, int lapNo1A, String name2A, double time2A, int lapNo2A) {
this.placeA = placeA;
this.venue1A = venue1A;
this.venue2A = venue2A;
this.name1A = name1A;
this.time1A = time1A;
this.lapNo1A = lapNo1A;
this.name2A = name2A;
this.time2A = time2A;
this.lapNo2A = lapNo2A;
}
public String getPlaceA() {
return placeA;
}
public void setPlaceA(String placeA) {
this.placeA = placeA;
}
public String getVenue1A() {
return venue1A;
}
public String getVenue2A() {
return venue2A;
}
public String getName1A() {
return name1A;
}
public void setName1A(String name1A) {
this.name1A = name1A;
}
public double getTime1A() {
return time1A;
}
public void setTime1A(double time1) {
this.time1A = time1A;
}
public int getLapNo1A() {
return lapNo1A;
}
public void setLapNo1A(int lapNo1A) {
this.lapNo1A = lapNo1A;
}
public String getName2A() {
return name2A;
}
public void setName2A(String name2A) {
this.name2A = name2A;
}
public double getTime2() {
return time2A;
}
public void setTime2(double time2) {
this.time2A = time2;
}
public int getLapNo2() {
return lapNo2A;
}
public void setLapNo2(int lapNo2) {
this.lapNo2A = lapNo2;
}
}
Class B:
/*
* Racers with
* Max Age : none
* Max horsepower : 850
*/
public class Class_B {
//Instance variables
private String placeB;
private final String venue1B;
private final String venue2B;
private String name1B;
private double time1B;
private int lapNo1B;
private String name2B;
private double time2B;
private int lapNo2B;
public Class_B(String placeB, String venue1B, String venue2B, String name1B, double time1B, int lapNo1B, String name2B, double time2B, int lapNo2B) {
this.placeB = placeB;
this.venue1B = venue1B;
this.venue2B = venue2B;
this.name1B = name1B;
this.time1B = time1B;
this.lapNo1B = lapNo1B;
this.name2B = name2B;
this.time2B = time2B;
this.lapNo2B = lapNo2B;
}
public String getPlaceB() {
return placeB;
}
public void setPlaceB(String placeB) {
this.placeB = placeB;
}
public String getVenue1B() {
return venue1B;
}
public String getVenue2B() {
return venue2B;
}
public String getName1B() {
return name1B;
}
public void setName1B(String name1B) {
this.name1B = name1B;
}
public double getTime1B() {
return time1B;
}
public void setTime1B(double time1B) {
this.time1B = time1B;
}
public int getLapNo1B() {
return lapNo1B;
}
public void setLapNo1B(int lapNo1B) {
this.lapNo1B = lapNo1B;
}
public String getName2B() {
return name2B;
}
public void setName2B(String name2B) {
this.name2B = name2B;
}
public double getTime2B() {
return time2B;
}
public void setTime2B(double time2B) {
this.time2B = time2B;
}
public int getLapNo2B() {
return lapNo2B;
}
public void setLapNo2B(int lapNo2B) {
this.lapNo2B = lapNo2B;
}
}