JavaFX Tableview on another screen


JavaFX Tableview on another screen



I am attempting to show a populated tableview on another controller in JavaFX using an example demo.



I have a model and view folder. There are three classes: In model folder: Main.java, DisplayContactListController.java, and Contact.java (data model).



in view folder: main.fxml (which has a button that goes to DisplayContactList.fxml) and DisplayContactList.fxml.



I need my table view to show on DisplayContactList.fxml, right now the page loads when I click the button but its blank... no tableview.


public class DisplayContactListController extends Application {
@FXML
private TableView<Contact> tvContactsList;

@Override
public void start(Stage primaryStage) {
Label response = new Label("");
Label title = new Label("Contact List Using a TableViewn");
title.setFont(Font.font("Arial", FontWeight.BOLD, 20));
title.setTextFill(Color.CADETBLUE);

FlowPane root = new FlowPane();
root.setAlignment(Pos.CENTER);

Scene scene = new Scene(root, 450, 450);

ObservableList<Contact> contactList = FXCollections.observableArrayList(
new Contact("Peggy", "Fisher", "717-555-1212"),
new Contact("Jim", "Freed", "441-456-1345"),
new Contact("Pat", "Keegan", "717-363-1432"),
new Contact("Jane", "Slattery", "441-478-4488"),
new Contact("Cy", "Young", "970-554-1265"),
new Contact("Rob", "Jones", "570-655-1587"),
new Contact("Carol", "King", "215-547-5958"),
new Contact("Bob", "Kauffman", "215-456-6345"),
new Contact("Gloria", "Shilling", "717-785-6092"),
new Contact("Bill", "Sigler", "441-444-1345")
);

tvContactsList = new TableView<>(contactList);

TableColumn<Contact, String> fName = new TableColumn<>("First Name");
fName.setCellValueFactory(new PropertyValueFactory<>("firstName"));
tvContactsList.getColumns().add(fName);

TableColumn<Contact, String> lName = new TableColumn<>("Last Name");
lName.setCellValueFactory(new PropertyValueFactory<>("lastName"));
tvContactsList.getColumns().add(lName);

TableColumn<Contact, String> cell = new TableColumn<>("Cell Phone Number");
cell.setCellValueFactory(new PropertyValueFactory<>("cellPhone"));
tvContactsList.getColumns().add(cell);

tvContactsList.setPrefWidth(300);
tvContactsList.setPrefHeight(300);

TableView.TableViewSelectionModel<Contact> tvSelContact =
tvContactsList.getSelectionModel();

tvSelContact.selectedIndexProperty().addListener((ObservableValue<? extends Number> changed, Number oldVal, Number newVal) -> {
int index = (int)newVal;
response.setText("The cell number for the contact selected is "
+contactList.get(index).getCellPhone());
});

response.setFont(Font.font("Arial", 14));
root.getChildren().addAll(title,tvContactsList, response);
primaryStage.setTitle("Contact List");
primaryStage.setScene(scene);
primaryStage.show();
}
}



How do I get the table view to show on a secondary page?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages