I have been trying out to create a tableview in javafx. For some reason, the tableview has the right columns, but doesn't have any data filled in it. Here's what i've tried so far. I have also tried the code on Eclipse and Intellij so I don't think there is any issue with my compiler or environment. Thanks for the help in advance. :)
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.*;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.collections.*;
public class MainScreen extends Application{
private TableView<Item> table = new TableView<Item>();
private final ObservableList<Item> data =
FXCollections.observableArrayList(new Item("AZZZZ", "3"),new Item("ASD","33"));
public static void main(String args[]) {
launch(args);
}
public void start(Stage stage) {
stage.setTitle("Inventory Management");
FlowPane root = new FlowPane();
table.setEditable(true);
TableColumn<Item,String> nameCol = new TableColumn<>("Name");
TableColumn<Item,String> qtyCol = new TableColumn<>("Quantity");
nameCol.setMinWidth(100);
nameCol.setCellValueFactory(
new PropertyValueFactory<>("name"));
qtyCol.setMinWidth(100);
qtyCol.setCellValueFactory(
new PropertyValueFactory<>("number"));
//data.add(new Item("ZZZ","5"));
table.setItems(data);
table.getColumns().addAll(nameCol,qtyCol);
root.getChildren().addAll(table);
stage.setScene(new Scene(root,300,300));
stage.show();
}
public static class Item{
String name;
String number;
Item(String n,String num){
name=n;
number=num;
}
}
}
}