Results 1 to 10 of 10
Like Tree4Likes
  • 1 Post By benji2505
  • 1 Post By benji2505
  • 1 Post By benji2505
  • 1 Post By benji2505

Thread: Show Name by Id column tableview

  1. #1
    maurikius is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Show Name by Id column tableview

    Hello everyone, first of all I introduce myself, my name is Maurikius and I am a programmer, I'm currently trying to update some swing fx projects, I have a problem with a table view, I have a SALES model where I store among other things the id of the client, but in table view want the customer name is displayed, and I have a method that returns me the name through the ID, but being linked with model table not know how to do it manually. I put some screenshots.

    Show Name by Id column tableview-javafx.jpg

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

    Default Re: Show Name by Id column tableview

    My understanding is that you want to add a column in your list that contains the customer name. Here is what I would try to do:

    1. Add the column in the fxml file. This can be done in the SceneBuilder App (?), but you can also do it manually in the fxml file, maybe copy-paste of a similar column with Types<Sale,String> helps here.
    2. Announce the column in the global definitions with @FXML annotation (your top right hand picture)
    3. Create a StringProperty customerName. Fill it by looking up the name that corresponds to the customer id. (Bottom left+getter in customer class).
    4. Create a new cellValueFactory for your new column from 2. And stuff the customerName in there, analogue to the picture in bottom right corner.

    There might be some adjustments depending on how the program was designed so far.
    Last edited by benji2505; 10-02-2016 at 09:14 PM. Reason: String property vs. StringProperty
    maurikius likes this.

  3. #3
    maurikius is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Re: Show Name by Id column tableview

    Quote Originally Posted by benji2505 View Post
    My understanding is that you want to add a column in your list that contains the customer name. Here is what I would try to do:

    1. Add the column in the fxml file. This can be done in the SceneBuilder App (?), but you can also do it manually in the fxml file, maybe copy-paste of a similar column with Types<Sale,String> helps here.
    2. Announce the column in the global definitions with @FXML annotation (your top right hand picture)
    3. Create a StringProperty customerName. Fill it by looking up the name that corresponds to the customer id. (Bottom left+getter in customer class).
    4. Create a new cellValueFactory for your new column from 2. And stuff the customerName in there, analogue to the picture in bottom right corner.

    There might be some adjustments depending on how the program was designed so far.


    First of all thanks for the reply, what I want is that the name is displayed instead of ID, a problem is that the name is the other class, Customer class. Java Swing, had no problems using the method that is in the bottom left and added to the cell.

    This is the Customer class:

    Java FX Code:
    public class Customer {
    
        private IntegerProperty idCustomer;
        private StringProperty firstNameCustomer;
        private StringProperty lastNameCustomer;
        private StringProperty nifCustomer;
        private StringProperty birthdayCustomer;
        private StringProperty addressCustomer;
        private IntegerProperty postalCodeCustomer;
        private IntegerProperty phoneCustomer;
    
        public Customer() {
            super();
        }
    
        public Customer(Integer idCustomer, String firstNameCustomer, String lastNameCustomer, String nifCustomer,
                String birthdayCustomer, String addressCustomer, Integer postalCodeCustomer, Integer phoneCustomer) {
            super();
            this.idCustomer = new SimpleIntegerProperty(idCustomer);
            this.firstNameCustomer = new SimpleStringProperty(firstNameCustomer);
            this.lastNameCustomer = new SimpleStringProperty(lastNameCustomer);
            this.nifCustomer = new SimpleStringProperty(nifCustomer);
            this.birthdayCustomer = new SimpleStringProperty(birthdayCustomer);
            this.addressCustomer = new SimpleStringProperty(addressCustomer);
            this.postalCodeCustomer = new SimpleIntegerProperty(postalCodeCustomer);
            this.phoneCustomer = new SimpleIntegerProperty(phoneCustomer);
        }
    
        public Integer getIdCustomer() {
            return idCustomer.get();
        }
    
        public void setIdCustomer(Integer idCustomer) {
            this.idCustomer = new SimpleIntegerProperty(idCustomer);
        }
    
        public String getFirstNameCustomer() {
            return firstNameCustomer.get();
        }
    
        public void setFirstNameCustomer(String firstNameCustomer) {
            this.firstNameCustomer = new SimpleStringProperty(firstNameCustomer);
        }
    
        public String getLastNameCustomer() {
            return lastNameCustomer.get();
        }
    
        public void setLastNameCustomer(String lastNameCustomer) {
            this.lastNameCustomer = new SimpleStringProperty(lastNameCustomer);
        }
    
        public String getNifCustomer() {
            return nifCustomer.get();
        }
    
        public void setNifCustomer(String nifCustomer) {
            this.nifCustomer = new SimpleStringProperty(nifCustomer);
        }
    
        public String getBirthdayCustomer() {
            return birthdayCustomer.get();
        }
    
        public void setBirthdayCustomer(String birthdayCustomer) {
            this.birthdayCustomer = new SimpleStringProperty(birthdayCustomer);
        }
    
        public String getAddressCustomer() {
            return addressCustomer.get();
        }
    
        public void setAddressCustomer(String addressCustomer) {
            this.addressCustomer = new SimpleStringProperty(addressCustomer);
        }
    
        public Integer getPostalCodeCustomer() {
            return postalCodeCustomer.get();
        }
    
        public void setPostalCodeCustomer(Integer postalCodeCustomer) {
            this.postalCodeCustomer = new SimpleIntegerProperty(postalCodeCustomer);
        }
    
        public Integer getPhoneCustomer() {
            return phoneCustomer.get();
        }
    
        public void setPhoneCustomer(Integer phoneCustomer) {
            this.phoneCustomer = new SimpleIntegerProperty(phoneCustomer);
        }
    }


    I understand that the column would have to be how fxml <Customer, String>?
    Last edited by maurikius; 10-02-2016 at 09:24 PM.

  4. #4
    maurikius is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Re: Show Name by Id column tableview

    My code in Java Swing

    Java FX Code:
    public void showTableSale() {
            // Vacio la tabla//
            modelTableSale.setRowCount(0);
            // Cargo la tabla por defecto
            String customerName = null;
            String employeeName = null;
            String paymentMethodName = null;
            SaleDAO sale = saleFactory.createSaleDAO();
            CustomerDAO customer = customerFactory.createCustomerDAO();
            EmployeeDAO employee = employeeFactory.createEmployeeDAO();
            PaymentMethodDAO paymentMethod = paymentMethodFactory.createPaymentMethodDAO();
    
            try {
                // Cargo las facturas
                for (Sale s : sale.showAllSale()) {
                    // Obtengo el nombre y apellido por id
                    for (Customer cust : customer.getCustomerById(s.getIdCustomer())) {
                        customerName = cust.getFirstNameCustomer() + " " + cust.getLastNameCustomer();
                    }
                    // Obtengo el nombre del vendedor
                    for (Employee empl : employee.getEmployeeById(s.getIdEmployee())) {
                        employeeName = empl.getFirstNameEmployee() + " " + empl.getLastNameEmployee();
                    }
    
                    // Obtengo el nombre del pago por id
                    for (PaymentMethod pay : paymentMethod.getPaymentMethodById(s.getIdPaymentMethod())) {
                        paymentMethodName = pay.getNamePaymentMethod();
                    }
    
                    modelTableSale.addRow(
                            new Object[]{s.getIdSale(), s.getDocumentSale(), s.getDateSale(), customerName, paymentMethodName, s.getStateSale(), employeeName});
                }
    
            } catch (SQLException ex) {
                Logger.getLogger(ManageCategoryView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
        }

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

    Default Re: Show Name by Id column tableview

    My suggestion is going into into the direction to add the name column first, then check for consistency, then erase the customer id column.

    Your programm is set up in a way that the columns in the table are fed by a cellValueFactory, which is the default way in JavaFx. The cellValueFactory injects property values based on the displayed instance of Sale. (I think it also the only way).

    If I had to show the name, I would add a StringProperty in the Sale class and put the name string in there by
    1. Get the customer instance with your bottom left method and
    2. Call the getter for the name on this customer instance. There should be one in the Customer class.

    The interaction between Java and FXML introduces some changes. But it has some advantages, the table should be dynamically updated now. You will not be able to just plug in the name string into the customer id column, because the cellValueFactory expects something with the parameters <Sale, Integer>.

    'Hope that makes sense.
    Last edited by benji2505; 10-03-2016 at 01:06 AM.
    maurikius likes this.

  6. #6
    maurikius is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Re: Show Name by Id column tableview

    Quote Originally Posted by benji2505 View Post
    My suggestion is going into into the direction to add the name column first, then check for consistency, then erase the customer id column.

    Your programm is set up in a way that the columns in the table are fed by a cellValueFactory, which is the default way in JavaFx. The cellValueFactory injects property values based on the displayed instance of Sale. (I think it also the only way).

    If I had to show the name, I would add a StringProperty in the Sale class and put the name string in there by
    1. Get the customer instance with your bottom left method and
    2. Call the getter for the name on this customer instance. There should be one in the Customer class.

    The interaction between Java and FXML introduces some changes. But it has some advantages, the table should be dynamically updated now. You will not be able to just plug in the name string into the customer id column, because the cellValueFactory expects something with the parameters <Sale, String>.

    'Hope that makes sense.

    And to do that you say, would also store the customer name in the database is not? in Table Sale. Or conversely could calculate the name in the Customer class and return the name, ie, the method below left instantiate get him in class customer with getter and setter.

  7. #7
    maurikius is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Re: Show Name by Id column tableview

    Quote Originally Posted by benji2505 View Post
    My suggestion is going into into the direction to add the name column first, then check for consistency, then erase the customer id column.

    Your programm is set up in a way that the columns in the table are fed by a cellValueFactory, which is the default way in JavaFx. The cellValueFactory injects property values based on the displayed instance of Sale. (I think it also the only way).

    If I had to show the name, I would add a StringProperty in the Sale class and put the name string in there by
    1. Get the customer instance with your bottom left method and
    2. Call the getter for the name on this customer instance. There should be one in the Customer class.

    The interaction between Java and FXML introduces some changes. But it has some advantages, the table should be dynamically updated now. You will not be able to just plug in the name string into the customer id column, because the cellValueFactory expects something with the parameters <Sale, String>.

    'Hope that makes sense.

    Within the class sale, I have used the method of the part bottom left, and is running ok.
    That you referred to?
    Thanks a lot.

    Class SALE

    Java FX Code:
    public String getNameCustomer() {
            try {
                int id = idCustomer.getValue();
                CustomerDAO cd = new CustomerDAOImpl();
                nameCustomer = new SimpleStringProperty(cd.getCustomerById(id).getFirstNameCustomer());
            } catch (SQLException ex) {
                Logger.getLogger(Sale.class.getName()).log(Level.SEVERE, null, ex);
            }
            return nameCustomer.get();
        }

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

    Default Re: Show Name by Id column tableview

    Almost. The JavaFx table is nothing more than a database table, in that sense you are right. But you do not have to change the database structure or the Customer class.
    The JavaFx table wants to display a name string specific to a Sale instance. But so far your sale instance does not have that name string. It only has the customer id. This thinking comes from the database world that tries to minimize data baggage, knowing that it can always look it up. This why you have to create the name StringProperty here.
    All the changes could be made in the Sale class. It should look something like this:
    Java FX Code:
    private StringProperty customerName;
    ...
    Customer dudethatboughtcar = (Customer)getCustomerById(customerID);
    customerName = dudethatboughtcar.getFirstName() + " " + dudethatboughtcar.getLastName();
    And then in your FxController class you just add the line
    YourNewColumn.setValueFactory............("custome rName"));
    Last edited by benji2505; 10-02-2016 at 11:13 PM.
    maurikius likes this.

  9. #9
    maurikius is offline Member
    Join Date
    Oct 2016
    Posts
    6
    Rep Power
    0

    Default Re: Show Name by Id column tableview

    Quote Originally Posted by benji2505 View Post
    Almost. The JavaFx table is nothing more than a database table, in that sense you are right. But you do not have to change the database structure or the Customer class.
    The JavaFx table wants to display a name string specific to a Sale instance. But so far your sale instance does not have that name string. It only has the customer id. This thinking comes from the database world that tries to minimize data baggage, knowing that it can always look it up. This why you have to create the name StringProperty here.
    All the changes could be made in the Sale class. It should look something like this:
    Java FX Code:
    private StringProperty customerName;
    ...
    Customer dudethatboughtcar = (Customer)getCustomerById(customerID);
    customerName = dudethatboughtcar.getFirstName() + " " + dudethatboughtcar.getLastName();
    And then in your FxController class you just add the line
    YourNewColumn.setValueFactory............("custome rName"));
    Thank you friend, it is already working properly, I put the code in case anyone needs it in the future.

    Class Sale :

    Java FX Code:
    private IntegerProperty idSale;
        private IntegerProperty idCustomer;
        private IntegerProperty idEmployee;
        private StringProperty stateSale;
        private StringProperty dateSale;
        private StringProperty documentSale;
        private IntegerProperty idPaymentMethod;
        private DoubleProperty discountSale;
        private DoubleProperty totalSale;
    
        // Solo tabla
        private StringProperty nameCustomer;
        private StringProperty nameEmployee;
        private StringProperty namePaymentMethod;
    
        // Solo para la tabla
        public String getNameCustomer() {
            try {
                int id = idCustomer.getValue();
                CustomerDAO cd = new CustomerDAOImpl();
                Customer customer = cd.getCustomerById(id);
                String firstName = customer.getFirstNameCustomer();
                String lastName = customer.getLastNameCustomer();
                nameCustomer = new SimpleStringProperty(firstName + " " + lastName);
            } catch (SQLException ex) {
                Logger.getLogger(Sale.class.getName()).log(Level.SEVERE, null, ex);
            }
            return nameCustomer.get();
        }
    
        public String getNameEmployee() {
            try {
                int id = idEmployee.getValue();
                EmployeeDAO ed = new EmployeeDAOImpl();
                Employee employee = ed.getEmployeeById(id);
                String firstName = employee.getFirstNameEmployee();
                String lastName = employee.getLastNameEmployee();
                nameEmployee = new SimpleStringProperty(firstName + " " + lastName);
            } catch (SQLException ex) {
                Logger.getLogger(Sale.class.getName()).log(Level.SEVERE, null, ex);
            }
            return nameEmployee.get();
        }
    
        public String getNamePaymentMethod() {
            try {
                int id = idPaymentMethod.getValue();
                PaymentMethodDAO pmd = new PaymentMethodDAOImpl();
                PaymentMethod paymentMethod = (PaymentMethod) pmd.getPaymentMethodById(id);
                namePaymentMethod = new SimpleStringProperty(paymentMethod.getNamePaymentMethod());
            } catch (SQLException ex) {
                Logger.getLogger(Sale.class.getName()).log(Level.SEVERE, null, ex);
            }
            return namePaymentMethod.get();
        }

    View:

    Java FX Code:
    @FXML
        private TableView<Sale> tableShowSale;
        @FXML
        private TableColumn<Sale, Integer> colTableIdSale;
        @FXML
        private TableColumn<Sale, String> colTableCustomerSale;
        @FXML
        private TableColumn<Sale, String> colTableEmployeeSale;
        @FXML
        private TableColumn<Sale, String> colTableStateSale;
        @FXML
        private TableColumn<Sale, String> colTableDateSale;
        @FXML
        private TableColumn<Sale, String> colTableDocumentSale;
        @FXML
        private TableColumn<Sale, String> colTableMethodPaymentSale;
        @FXML
        private TableColumn<Sale, Integer> colTableDiscountSale;
        @FXML
        private TableColumn<Sale, Integer> colTableTotalSale;
    
    
    public void initialize(URL url, ResourceBundle rb) {
    
            try {
                // Añado los productos la tabla
                SaleDAO pd = new SaleDAOImpl();
                listSale = pd.showAllSale();
                tableShowSale.setItems(listSale);
    
                // Indico a las columnas los datos a mostrar
                colTableIdSale.setCellValueFactory(new PropertyValueFactory<>("idSale"));
                colTableCustomerSale.setCellValueFactory(new PropertyValueFactory<>("nameCustomer"));
                colTableEmployeeSale.setCellValueFactory(new PropertyValueFactory<>("nameEmployee"));
                colTableStateSale.setCellValueFactory(new PropertyValueFactory<>("stateSale"));
                colTableDateSale.setCellValueFactory(new PropertyValueFactory<>("dateSale"));
                colTableDocumentSale.setCellValueFactory(new PropertyValueFactory<>("documentSale"));
                colTableMethodPaymentSale.setCellValueFactory(new PropertyValueFactory<>("namePaymentMethod"));
                colTableDiscountSale.setCellValueFactory(new PropertyValueFactory<>("discountSale"));
                colTableTotalSale.setCellValueFactory(new PropertyValueFactory<>("totalSale"));
                ....

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

    Default Re: Show Name by Id column tableview

    Nice. Congrats. Time for a cold one. 👍😀
    maurikius likes this.

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. Replies: 0
    Last Post: 01-17-2013, 12:43 PM
  4. “Extra Column” in TableView
    By pugazhendhiobs in forum JavaFX
    Replies: 2
    Last Post: 03-24-2012, 07:22 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
  •