Thursday, April 7, 2016

TABLE VIEW IN JAVAFX


TABLE VIEW IN JAVAFX


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;


public class CalculationView extends VBox
{
    private TableView table;
    private TableColumn sno,room_no,name,no_ofdays,extra,guest,penalty,test;
    private MainView mv;
    private Label title;
     private final ObservableList<Person> data =FXCollections.observableArrayList();
    public CalculationView()
    {
        //this.mv=main;
     instanceCreation();
     setdata();
     designform();
   
    }
    private void instanceCreation()
    {
        //MainTable
        table=new TableView();
         //table.setEditable(true);
       
        //Table Column
        sno=new TableColumn();
        sno.setMinWidth(100);
        sno.setCellValueFactory(
                new PropertyValueFactory<Person, String>("sNo"));
       
        room_no=new TableColumn();
        room_no.setMinWidth(100);
        room_no.setCellValueFactory(
                new PropertyValueFactory<Person, String>("roomNo"));
       
        name=new TableColumn();
        name.setMinWidth(200);
        name.setCellValueFactory(
                new PropertyValueFactory<Person, String>("fname"));
   
        no_ofdays=new TableColumn();
        no_ofdays.setMinWidth(180);
        no_ofdays.setCellValueFactory(new PropertyValueFactory<Person, TextField>("noDays"));
       
        extra=new TableColumn();
        extra.setMinWidth(150);
        extra.setCellValueFactory(new PropertyValueFactory<Person, TextField>("extraChrg"));
       
        guest=new TableColumn();
        guest.setMinWidth(150);
        guest.setCellValueFactory(new PropertyValueFactory<Person, TextField>("guestChrg"));
       
        penalty=new TableColumn();
        penalty.setMinWidth(150);
        penalty.setCellValueFactory(new PropertyValueFactory<Person, TextField>("penaltyChrg"));
       
        test=new TableColumn();
        test.setMinWidth(100);
        test.setCellValueFactory(new PropertyValueFactory<Person, String>("test"));
       
        title=new Label();   
    }
    private void setdata()
    {
          for(int i=0;i<100;i++){
              //  data.add(new Person(String.valueOf(i),"test"+i,""+i+i+"", "name"+i));
              data.add(new Person(String.valueOf(i),""+i+i+"","name"+i,"i","i","i","i","tset1"));
           
            }
        //columns data
        sno.setText("S.NO");
        room_no.setText("ROOM NO");
        name.setText("NAME");
        no_ofdays.setText("NO OF DAYS FOOD TAKEN");
        extra.setText("EXTRA MESS CHARGE");
        guest.setText("GUEST CHARGE");
        penalty.setText("PENALTY CHARGE");
        test.setText("Test col");
        //titles
        title.setText("CHARGES TABLE");
        title.setFont(new Font("Arialblack", 20));
       
    }
    private void designform()
    {       
        table.getColumns().addAll(sno,room_no,name,no_ofdays,extra,guest,penalty,test);
        this.setStyle("-fx-border-color: red");
        //this.setPadding(new Insets(0,0,0,99));
        this.getChildren().addAll(title,table);
        table.setItems(data);
   
    }
   

}

Person CLASS FOR TO RETRIVE TABLE DATA(GETTER AND SETTER METHODS)

 

 

import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.TextField;


public class Person
{
    private TextField noDays,extraChrg,guestChrg,penaltyChrg;
    private SimpleStringProperty sNo;
    private SimpleStringProperty roomNo;
    private SimpleStringProperty fname;
    private SimpleStringProperty test;
   
   
    public Person(String sNo,String roomno,String fname,String no,String ex,String gs,String pen,String test)
    {
        this.roomNo=new SimpleStringProperty(roomno);
        System.out.println(sNo);
        this.sNo= new SimpleStringProperty(sNo);
        this.fname=new SimpleStringProperty(fname);
        this.test=new SimpleStringProperty(test);
        this.noDays=new TextField(no);
        this.extraChrg=new TextField(ex);
        this.guestChrg=new TextField(gs);
        this.penaltyChrg=new TextField(pen);
      
      
    }
    public TextField getNoDays() {
        return noDays;
    }
    public void setNoDays(TextField noDays) {
        this.noDays = noDays;
    }
    public TextField getExtraChrg() {
        return extraChrg;
    }
    public void setExtraChrg(TextField extraChrg) {
        this.extraChrg = extraChrg;
    }
    public TextField getGuestChrg() {
        return guestChrg;
    }
    public void setGuestChrg(TextField guestChrg) {
        this.guestChrg = guestChrg;
    }
    public TextField getPenaltyChrg() {
        return penaltyChrg;
    }
    public void setPenaltyChrg(TextField penaltyChrg) {
        this.penaltyChrg = penaltyChrg;
    }


    public String getSNo() {
        return sNo.get();
    }
    public void setsNo(String sNo)
    {
        this.sNo.set(sNo);
    }
   


    public String getRoomNo() {
        return roomNo.get();
    }
    public String gettest()
    {
        return test.get();
    }
    public void settest(String testval)
    {
        test.set(testval);
    }

   
    public String getFname() {
        return fname.get();
    }
    
   

}

 


 
 


NOTE:


According to the PropertyValueFactorydocumentation..,
Make sure to type correct getter and setter method with respect to variable name.
If variable name is abc means,your methods should be like this getAbc(),setAbc().

No comments:

Post a Comment