0

I had a bit of an issue when showing my data on the table view I can't get the data to show on the table. As I want to do it through fxml file but seem like when I complete it didn't show up any data in the table. Maybe my syntax was wrong? Can I have feedback on my code to improve it, please?

This is my fxml file.

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.text.*?>

<VBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MembershipController" styleClass="background" prefHeight="400.0" prefWidth="600.0" spacing="20">
    <padding>
        <Insets top="10" bottom="10" left="5" right="5" />
    </padding>
    
    <Label styleClass="label-header" text="Filters"/>
    
    <HBox alignment="CENTER" spacing="20" >
        <Label styleClass="label-opac" text="Name"/>
        <TextField fx:id="nameFilterTf" text="Filter by name"/>
        <Label styleClass="label-opac" text="Email"/>
        <TextField fx:id="emailFilterTf" text="Filter by email"/> 
    </HBox>
    
    <TableView fx:id="membershipTv" items="${controller.membership.Memberships}" styleClass="table-view" scaleShape="true" > 
        <placeholder>
            <Text text="No accounts found"/>
        </placeholder>
        <columns>
            <TableColumn fx:id="nameClm" text="Name">
                <cellValueFactory><PropertyValueFactory property="name"/></cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="emailClm" text="Email">
                <cellValueFactory><PropertyValueFactory property="email"/></cellValueFactory>
            </TableColumn>
            <TableColumn fx:id="phoneClm" text="Phone">
                <cellValueFactory><PropertyValueFactory property="phone"/></cellValueFactory>
            </TableColumn>
        </columns>
    </TableView>
    
    <HBox spacing="10" alignment="CENTER" scaleShape="true"  >
        <Button styleClass="button" fx:id="addBtn" text="Add"/>
        <Button styleClass="button" fx:id="deleteBtn" text="Delete"/>
        <Button styleClass="button" fx:id="selecteBtn" text="Select"/>
        <Button styleClass="button" fx:id="slipBtn" text="SLIP"/>
        <Button styleClass="button" fx:id="reportBtn" text="Report"/>
        <Button styleClass="button" fx:id="closeBtn" text="Close"/>
    </HBox>
    
    <stylesheets>
        <URL value="@mmsfx.css"/>
    </stylesheets>
    
</VBox>

This is my controller.

    package controller;

import au.edu.uts.ap.javafx.Controller;
import au.edu.uts.ap.javafx.ViewLoader;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.*;
import model.Membership;
import model.Memberships;


public class MembershipController extends Controller<MembershipController> {

    public ObservableList<model.Membership> memberLs;
    @FXML private TableView<model.Membership> membershipTv;
    @FXML private TableColumn<model.Membership, String> nameClm;
    @FXML private TableColumn<model.Membership, String> emailClm;
    @FXML private TableColumn<model.Membership, String> phoneClm;
    
    
    @FXML Button addBtn;
    @FXML Button deleteBtn;
    @FXML Button selecteBtn;
    @FXML Button slipBtn;
    @FXML Button reportBtn;
    @FXML Button closeBtn;
    
    @FXML private void intialize(URL url, ResourceBundle resource){
        model.Memberships memberList = new model.Memberships();        
        memberLs = memberList.getCurrentList();
        nameClm.setCellValueFactory(new PropertyValueFactory<model.Membership, String>("name"));
        emailClm.setCellValueFactory(new PropertyValueFactory<model.Membership, String>("email"));
        phoneClm.setCellValueFactory(new PropertyValueFactory<model.Membership, String>("phone"));
        membershipTv.setItems(memberList.getCurrentList());
    }
}

This is the Memberships Class.

public class Memberships {

    private ObservableList<Membership> Memberships;
    private ObservableList<Membership> current;

    public Memberships() {
        current = FXCollections.<Membership>observableArrayList();
        Memberships = FXCollections.observableArrayList(
        new Membership("Thomas Muller", "thomas.muller@uts.com", "99991111", "3 Byern St. Sydney 2001", "13697480", 2134.5),
        new Membership("Alice Stefan", "alice.stefan@uts.com", "88881111", "24 Pitt St. Sydney 2001", "14517800", 4512.2),
        new Membership("Lucy Lu", "lucy.lu@uts.com", "98981100", "11 Hunter St. Sydney 2100", "13267102", 158.4),
        new Membership("Andreas Brehme", "andreas.b@uts.com", "90001222", "91 Sussex St. Sydney 2100", "13678200", 7596.3),
        new Membership("Ruddy Voller", "ruddy.v@uts.com", "98980000", "15 Stan St. Sydney 2100", "13972870", 1100.0),
        new Membership("Monica Shwarz", "monica.s@uts.com", "92241188", "155 Jones St. Sydney 2001", "13859610", 6741.2)
        );
        current.addAll(Memberships);
        Memberships.addListener(new ListChangeListener<Membership>() {
            @Override
            public void onChanged(javafx.collections.ListChangeListener.Change<? extends Membership> c) {
                current.clear();
                current.addAll(Memberships);
            }
        });
    }

    public void addMembership(Membership Membership) {
        Memberships.add(Membership);
    }

    public void addMembership(String name, String email, String phone, String address, String ID, double expense) {
        Memberships.add(new Membership(name,email,phone,address,ID,expense));
    }
    
    public boolean hasMembership(String name) {
        for(Membership e:Memberships)
            if(e.hasName(name)&& !name.isEmpty())
                return true;
        return false;
    }
    
    public Membership getMembership(String name){
        for(Membership p: Memberships)
            if(p.hasName(name))
                return p;
        return null;                        
    }

    public void remove(Membership p) {
        this.Memberships.remove(p);
    }

    public void remove(List<Membership> selectedItems) {
        this.Memberships.removeAll(selectedItems);
    }

    public void addMemberships(List<Membership> selectedItems) {
        this.Memberships.addAll(selectedItems);
    }

    public ObservableList<Membership> getCurrentList() {
        return this.current;
    }

    public void filterList(String name, String email) {

        List<Membership> temp = new ArrayList<>();

        Memberships.forEach((Membership p) -> {
            try {
                if ((p.hasName(name)) || (p.hasEmail(email))) {
                    temp.add(p);
                }
            } catch (NumberFormatException e) {
            }
        });

        this.current.clear();
        this.current.addAll(temp);
    }
}

and Membership Class.

    public class Membership {
    private StringProperty ID;
    private StringProperty name;
    private StringProperty email;
    private StringProperty phone;
    private StringProperty address;
    private StringProperty type;
    private DoubleProperty expense;
    private DoubleProperty payPerCredit;
    private DoubleProperty totalCredits;
    private DoubleProperty GasdeductionRate;
    private IntegerProperty DollarAvailable;
    private DoubleProperty DollarAvailable;
    private DoubleProperty deductionRate;
    private SuperMarket  SuperMarket;

    public Membership(String name, String email, String phone, String address, String ID,  double expense) 
{
        this.name = new SimpleStringProperty();
        this.name.set(name);
        this.email = new SimpleStringProperty();
        this.email.set(email);
        this.phone = new SimpleStringProperty();
        this.phone.set(phone);
        this.address = new SimpleStringProperty();
        this.address.set(address);
        this.ID = new SimpleStringProperty();
        this.ID.set(ID);
        this.expense = new SimpleDoubleProperty();
        this.expense.set(expense);   
        SetType();
        setPayPerCredit();
        this.totalCredits = new SimpleDoubleProperty();
        this.totalCredits.bind(this.expense.multiply(this.payPerCredit));
        this.DollarAvailable = new SimpleIntegerProperty();
        this.DollarAvailable.bind(this.totalCredits.divide(200));
        SetdeductionRate();
        SetGasdeductionRate();
    }

    public void SetType()
    {
        this.type = new SimpleStringProperty();
        if (getexpense()>=500 && getexpense()<1500)
        {
            this.type.set("Silver");
        }
        else if (getexpense()>=1500 && getexpense()<3000)
        {
           this.type.set("Gold");
        }
        else if (getexpense()>=3000 && getexpense()<5000)
        {
            this.type.set("Diamond");
        }
        else if (getexpense()>=5000)
        {
            this.type.set("Platinum");
        }
        else if (getexpense()==0)
        {
            this.type.set("None");
        }
        else
        {
            this.type.set("Bronze");
        }
    }
    public void setPayPerCredit()
    {
        this.payPerCredit = new SimpleDoubleProperty();
        switch(getType()){
            case "Bronze":
                this.payPerCredit.set(20.00);
                break;
            case "Silver":
                this.payPerCredit.set(10.00);
                break;
            case "Gold":
                this.payPerCredit.set(8.00);
                break;
            case "Diamond":
                this.payPerCredit.set(6.00);
                break;
            case "Platinum":
                this.payPerCredit.set(4.00);
                break;
            default :
                this.payPerCredit.set(30.00);
                break;
        }
    }
    public void SetdeductionRate()
    {
        this.deductionRate = new SimpleDoubleProperty();
        switch(getType()){
            case "Bronze":
                this.deductionRate.set(0.05);
                break;
            case "Silver":
                this.deductionRate.set(0.10);
                break;
            case "Gold":
                this.deductionRate.set(0.15);
                break;
            case "Diamond":
                this.deductionRate.set(0.20);
                break;
            case "Platinum":
                this.deductionRate.set(0.25);
                break;
        }
    }
    public void SetGasdeductionRate()
    {
        this.GasdeductionRate = new SimpleDoubleProperty();
        switch(getType()){
            case "Bronze":
                this.GasdeductionRate.set(0.10);
                break;
            case "Silver":
                this.GasdeductionRate.set(0.15);
                break;
            case "Gold":
                this.GasdeductionRate.set(0.20);
                break;
            case "Diamond":
                this.GasdeductionRate.set(0.25);
                break;
            case "Platinum":
                this.GasdeductionRate.set(0.30);
                break;
        }
    }
    public void updateDetails(String name, String email, String phone, String address, String ID,  double expense){
        this.name.set(name);    
        this.email.set(email);        
        this.phone.set(phone);     
        this.address.set(address);        
        this.ID.set(ID); 
        this.expense.set(this.expense.getValue()+expense);     
        SetType();
        setPayPerCredit();
        this.totalCredits.bind(this.expense.multiply(this.payPerCredit));
        this.DollarAvailable.bind(this.totalCredits.divide(200));
        SetdeductionRate();
        SetGasdeductionRate();

    }


    public void setSuperMarket(SuperMarket e){
        this.SuperMarket = e;
    }

    public SuperMarket getSuperMarket(){
        return this.SuperMarket;
    }
    public ReadOnlyStringProperty nameProperty() 
    {
        return name;
    }
    public String getName(){
        return name.getValue();
    }
    public ReadOnlyStringProperty emailProperty() {
        return email;
    }

    public String getEmail(){
        return email.getValue();
    }

    public ReadOnlyStringProperty phoneProperty() {
        return phone;
    }

    public String getPhone(){
        return phone.getValue();
    }
    
    public ReadOnlyStringProperty addressProperty() {
        return address;
    }

    public String getAddress(){
        return address.getValue();
    }
    
    public ReadOnlyStringProperty IDProperty() {
        return ID;
    }

    public String getID(){
        return ID.getValue();
    }

   public ReadOnlyStringProperty typeProperty() {
        return type;
    }

    public String getType(){
        return type.getValue();
    }
    public ReadOnlyDoubleProperty payPerCreditProperty() 
    {
        return payPerCredit;
    }    
    
    public double getPayPerCredit(){
        return payPerCredit.get();
    }
    public ReadOnlyDoubleProperty totalCreditsProperty() 
    {
        return totalCredits;
    }    
    
    public double gettotalCredits(){
        return totalCredits.get();
    }
    public IntegerProperty DollarAvailableProperty() 
    {
        return DollarAvailable;
    }
    
    public double getDollarAvailable(){
        return DollarAvailable.get();
    }

    public ReadOnlyDoubleProperty GasdeductionRateProperty() 
    {
        return GasdeductionRate;
    }    
    
    public double getGasdeductionRate()
    {
        return GasdeductionRate.get();
    }


    public ReadOnlyDoubleProperty payPerCreditProperty() 
   {
       return payPerCredit;
   }    
    
   public double getPayPerCredit()
  {
        return payPerCredit.get();
    }

    public ReadOnlyDoubleProperty deductionRateProperty() 
    {
        return deductionRate;
    }    
    public double getdeductionRate()
    {
        return deductionRate.get();
    }
    public ReadOnlyDoubleProperty expenseProperty() 
    {
        return expense;
    }    
    public double getexpense()
    {
        return expense.get();
    }


    public boolean hasName(String name){
        return getName().toLowerCase().contains(name.toLowerCase().trim());
    }

    public boolean hasEmail(String email){
        return getEmail().toLowerCase().contains(email.toLowerCase().trim());
    }

}
  • 2
    Not sure if these are the only issues: (1) Your setters should not be creating new property instances but should instead be setting the values of the already-existing property instances (in fact, I would suggest making all property fields `final`), and (2) some of your methods do not follow the proper naming conventions. – Slaw Oct 18 '22 at 08:35
  • 2
    Also see https://stackoverflow.com/q/72437983/2189127 – James_D Oct 18 '22 at 10:57

0 Answers0