Results 1 to 3 of 3
  1. #1
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default JavaFx TableView add POJO Objects

    All examples about Property Classes. I want to do this with pojo classes. I am using scene builder. There is my classes.

    Java FX Code:
    package educationManagement;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Driver extends Application {
    	
    	@Override
    	public void start(Stage primaryStage) throws Exception {
    		FXMLLoader loader = new FXMLLoader(getClass().getResource("/educationManagement/view/UserLogin.fxml")); //Fxml'in lokasyonunu belirtiyoruz.
    		Parent root = (Parent) loader.load(); //Parent'a cast ediyoruz. Çünkü Scene parent alıyor.
    		 Scene scene = new Scene(root);		   //Penceremizde çalışacak arayüzü oluşturup içine parentimizi atıyoruz.
    		 primaryStage.setTitle("Kullanıcı Girişi");
    		 primaryStage.setScene(scene);				   //Penceremize arayüzümü ekliyoruz.
    		 primaryStage.show();						   //Penceremizi show methoduyla görünebilir yapıyoruz.
    	}
    	
    	public static void main(String[] args) {
    		launch(args);
    	}
    }
    Java FX Code:
    package educationManagement.controller;
    
    import educationManagement.model.DBContext;
    import educationManagement.model.entity.Admin;
    import javafx.event.ActionEvent;
    import javafx.fxml.FXML;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Node;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.PasswordField;
    import javafx.scene.control.TextField;
    import javafx.stage.Stage;
    
    public class UserLoginController extends AbstractController {
    
        @FXML
        private Button joinBtn;
        @FXML
        private Button exitBtn;
        @FXML
        private Label statusLabel;
        @FXML
        private TextField userNo;
        @FXML
        private PasswordField password;
        
        
        @FXML
        void buttonEvent(ActionEvent event) {
        	
        	if (joinBtn == event.getSource()) {
        		String userType = userNo.getText().substring(0, 2);
        		switch (userType) {
        		case "10":
        			Admin admin = DBContext.getInstance().admins().isUserExist(Integer.parseInt(userNo.getText()), password.getText());
        			if(admin != null) {
        				final String fxml = "/educationManagement/view/AdminPanel.fxml";
        				try {
        					FXMLLoader loader = new FXMLLoader(getClass().getResource(fxml));
        					Parent root = (Parent) loader.load();
        					AdminController adminController = loader.getController();
        					adminController.setAdmin(admin);
        					adminController.load();
        					Scene scene = new Scene(root);
        					Stage stage = (Stage) ((Node)event.getSource()).getScene().getWindow();
        					stage.setTitle(admin.getFirstName() + " " + admin.getLastName());
        					stage.setScene(scene);
        					stage.show();
        				} catch (Exception e) {
        					e.printStackTrace();
        					System.out.println("fuck");
        				}
        				
        			} else {
        				statusLabel.setText("Kullanıcı Adı veya Şifre Yanlış");
        				userNo.setText("");
        				password.setText("");
        			}
        			break;
        			
        		case "40":
        			
        			break;
        			
        		case "50":
        			
        			break;
    
        		default:
        			statusLabel.setText("Kullanıcı Adı Bulunamadı");
        			break;
        		}
    
    		}
        }
    }
    Java FX Code:
    package educationManagement.controller;
    
    import java.net.URL;
    import java.util.Collection;
    import java.util.Date;
    import java.util.ResourceBundle;
    
    import educationManagement.model.DBContext;
    import educationManagement.model.entity.Admin;
    import educationManagement.model.entity.Student;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Label;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    
    public class AdminController implements Initializable{
    	
    	private Admin admin;
    
    	@FXML
    	private TableView<Student> studentTable;
    
    	@FXML
    	private TableColumn<Student, Integer> studentColNo;
    
    	@FXML
    	private TableColumn<Student, String> studentColTcNo;
    
    	@FXML
    	private TableColumn<Student, String> studentColFirstName;
    
    	@FXML
    	private TableColumn<Student, String> studentColLastName;
    
    	@FXML
    	private TableColumn<Student, Date> studentColStartDate;
    
    	@FXML
    	private TableColumn<Student, String> studentColCollege;
    
    	@FXML
    	private TableColumn<Student, String> studentColChapter;
    
    	@FXML
    	private TableColumn<Student, String> studentColMail;
    
        
        @FXML
        private Label firstNameLbl;
    
    	
    	
        public Admin getAdmin() {
    		return admin;
    	}
    	
        public void setAdmin(Admin admin) {
    		this.admin = admin;
    	}
        
        public void load() {
        	
        	
        }
    
    	@Override
    	public void initialize(URL location, ResourceBundle resources) {
    		Collection<Student> studentCollection = DBContext.getInstance().students().getAll();
        	ObservableList<Student> students = FXCollections.observableArrayList(studentCollection);
        	studentTable.setItems(students);
    	}
       
    }
    Java FX Code:
    package educationManagement.model.entity;
    
    import java.util.Date;
    
    public class User extends AbstractEntity{
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -4970273005866956586L;
    	private String tcNo;
    	private Date startDate;
    	private String firstName;
    	private String lastName;
    	private String password;
    	private College college;
    	private Contact contact;
    	
    	public String getTcNo() {
    		return tcNo;
    	}
    	public void setTcNo(String tcNo) {
    		this.tcNo = tcNo;
    	}
    	public Date getStartDate() {
    		return startDate;
    	}
    	public void setStartDate(Date startDate) {
    		this.startDate = startDate;
    	}
    	public String getFirstName() {
    		return firstName;
    	}
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getLastName() {
    		return lastName;
    	}
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public College getCollege() {
    		return college;
    	}
    	public void setCollege(College college) {
    		this.college = college;
    	}
    	
    	public Contact getContact() {
    		return contact;
    	}
    	public void setContact(Contact contact) {
    		this.contact = contact;
    	}
    	
    	@Override
    	public String toString() {
    		return "User [tcNo=" + tcNo + ", startDate=" + startDate + ", firstName=" + firstName + ", lastName=" + lastName
    				+ ", password=" + password + ", college=" + college + ", contact=" + contact + ", getId()=" + getId()
    				+ "]";
    	}
    
    }
    Java FX Code:
    package educationManagement.model.entity;
    
    public class Student extends User {
    	
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 2180158335050428116L;
    	private Chapter chapter;
    	
    	public Chapter getChapter() {
    		return chapter;
    	}
    	public void setChapter(Chapter chapter) {
    		this.chapter = chapter;
    	}
    	@Override
    	public String toString() {
    		return "Student [chapter=" + chapter + ", toString()=" + super.toString() + "]";
    	}

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: JavaFx TableView add POJO Objects

    You have the tables and the columns already set up in the controller class.

    I would think the question is "how do I fill it?", correct?

    1. create a Property for each Student Property that you want to display. This is usually done in a constructor. Something like
    this.lastName = new SimpleStringProperty(lastName);

    2. in the initialize method: hook each column in your table to a CellValueFactory and pass the property from 1. as parameter. Something like
    studentColLastName.setCellValueFactory(new PropertyValueFactory<>("lastName"));

    There was a post back last year on this forum that had a lot of sample code, which should give you a good idea as well.



    'hope that helps.

  3. #3
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    32
    Rep Power
    0

    Default Re: JavaFx TableView add POJO Objects

    Cre I solved with ur 2. answer. I didnt create property constroctur. But its work table not empty anymore. Thank you @benji2005

Similar Threads

  1. Replies: 4
    Last Post: 09-21-2016, 09:26 AM
  2. Selecting a row in a tableview
    By _Nikkiz_ in forum JavaFX
    Replies: 1
    Last Post: 07-31-2013, 04:50 AM
  3. JavaFX Book | Learn JavaFX More In Depth
    By SnakeDoc in forum JavaFX
    Replies: 0
    Last Post: 05-17-2013, 11:10 PM
  4. Replies: 0
    Last Post: 04-13-2012, 12:37 PM
  5. TableView problem
    By ZeCute in forum Android
    Replies: 0
    Last Post: 09-07-2011, 12:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •