100 Days Coding Challenge - Day 3/100 [User class dummy data relationship with TableView]

in #bigwaves6 years ago

Good evening steemians, I hope you had a great day today? It's TGIF today but I won't be partying due to the fact that there's a lot of development to be covered and I also have exams coming soon, so for now there won't be much time for playing.

A few fun things happened today, the best was going to Country Kitchen with friends, and guess what it was an all expense paid trip hehheehehehe, Alright so I had no idea I'd be going there cause it wasn't planned from the start of the day, I was going to visit a friend with @timmy-karis, we had gotten to the gate of her house when these two amazing humans came to meet us with a bike and asked us to take another bike and come with them, at first I thought it was a prank but then we got to country kitchen even with the rain... Yeah we all got a little bit wet but it was fun walking in the rain though.

I think the rest detail should be kept private for security reasons lol...


What did I do today?


In today's challenge I learned how to work with dummy data in relation to JavaFx TableView, made use of a User class, an observableArrayList, Controller class and the tableview. At the end of the day I was able to represent data from the user class in the table.

As you can see in the screenshot below, there's data contained in the table view.

image

The table is in place to display a list of customers currently in the Hotel and also allow the receptionist to select a customer for other actions to be carried out.
Testing this function with dummy data makes it easier to pull data from the database later in the progress of the development.


How did I come about this?


First I created a User Class in my program, in the user class the system would hold details of users in strings and any other variable type available. The user class will look like this

package Home;

import javafx.beans.property.SimpleStringProperty;

public class User {

    private final SimpleStringProperty firstname;
    private final SimpleStringProperty lastname;
    private final SimpleStringProperty telephone;
    private final SimpleStringProperty address;

public User(String fname, String lname, String tel, String resad){

    this.firstname = new SimpleStringProperty(fname);
    this.address = new SimpleStringProperty(resad);
    this.lastname = new SimpleStringProperty(lname);
    this.telephone = new SimpleStringProperty(tel);
}

    public String getFirstname() {
        return firstname.get();
    }
    public String getLastName(){

    return lastname.get();
    }
    public String getTelephone(){
    return telephone.get();
    }

    public String getAddress(){

    return address.get();
    }

    public void setFirstname(String fname) {
       firstname.set(fname);
    }
    public void setLastName(String lname){
lastname.set(lname);

    }
    public void setTelephone(String telp){
telephone.set(telp);
    }

    public void setAddress(String addres){
    address.set(addres);
    }
}


With each method either recieving a value or returning one, the User class can accept more users temporarily.. this means while the program is running, the user can add more details but these details will not be available once the system is ctoped. Secondlyy, the method allows the system draw information of users and display them in the table view.

Next:

In my controller class, I add a tableview, table column and an observable list... These three items are what we'll need for our code to work the right way.

  @FXML
    private TableView<User> table = new TableView();

    final ObservableList<User> data = FXCollections.observableArrayList(

            new User("John","Morisson","09045323454","Ipetu"),
            new User("Tony","Stark","09045323454","Example")

    );

    TableColumn column1 = new TableColumn("First Name");
    TableColumn column2 = new TableColumn("Last Name");
    TableColumn column3 = new TableColumn("Telephone");
    TableColumn column4 = new TableColumn("Address");

After this had been done, in the controller class, create a public method to initialize as below:

  public void initialize(){
        table.getColumns().removeAll();
        table.setItems(data);
        table.setEditable(true);

    column1.setMinWidth(100);
    column1.setCellValueFactory(new PropertyValueFactory<>("firstname"));


        column2.setMinWidth(100);
        column2.setCellValueFactory(new PropertyValueFactory<>("lastname"));


        column3.setMinWidth(100);
        column3.setCellValueFactory(new PropertyValueFactory<>("telephone"));


        column4.setMinWidth(100);
        column4.setCellValueFactory(new PropertyValueFactory<>("address"));

    table.getColumns().addAll(column1, column2, column3, column4);


    }

In this method, we can add items to both the table and its columns. "MinWidth" sets the width od the column to the specified size. while the PropertyValueFactory<> pulls data from the User Class and adds it to the column in the table.

image
At the end of the day today, even though I have not achieved as much as was supposed, I had a great day and I had so much fun also.

Tip for the Day:

No matter how bad the situation is, drive your happiness from God and within, only then will friends be able to help.

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63968.82
ETH 2633.99
USDT 1.00
SBD 2.84