jack jin
jack jin

Reputation: 1316

updateItem's second param have never be false in javafx2

I use javafx2 control TableView to dispay records in the database(I have 20 records now). And when display the record,I want to add button in each row. so I search lots of article use google,and write some code below.

the key code is:

    protected void updateItem(Long item, boolean empty) {
    super.updateItem(item, empty);
    if(empty) {
        setText(null);
        setGraphic(null);
    } else {
        final Button button = new Button("modifty");
        setGraphic(button);
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    }
}

I debug this code,the param "empty" would never be "false", so the button would never be display in the table view,

can anyone help me? thanks

below is the complete code(java and fxml):

java:

    package com.turbooo.restaurant.interfaces.javafx;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ResourceBundle;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.util.Callback;

import com.turbooo.restaurant.domain.Customer;
import com.turbooo.restaurant.domain.CustomerRepository;
import com.turbooo.restaurant.interfaces.facade.dto.CustomerDto;
import com.turbooo.restaurant.interfaces.facade.dto.assembler.CustomerDtoAssembler;
import com.turbooo.restaurant.util.ContextHolder;
import com.turbooo.restaurant.util.UTF8Control;

public class CustomerController implements Initializable {

    @FXML
    private TableView<CustomerDto> customerTableView;

    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {


        TableColumn<CustomerDto, Long> idCol = new TableColumn<CustomerDto, Long>("id");
        idCol.setCellValueFactory(
            new PropertyValueFactory<CustomerDto,Long>("id")
        );

        TableColumn<CustomerDto, String> nameCol = new TableColumn<CustomerDto, String>("name");
        nameCol.setMinWidth(100);
        nameCol.setCellValueFactory(
            new PropertyValueFactory<CustomerDto,String>("name")
        );

        TableColumn<CustomerDto, String> birthdayCol = new TableColumn<CustomerDto, String>("birthday");
        birthdayCol.setCellValueFactory(
                new Callback<TableColumn.CellDataFeatures<CustomerDto, String>, ObservableValue<String>>() {
                    @Override
                    public ObservableValue<String> call(TableColumn.CellDataFeatures<CustomerDto, String> customer) {
                        if (customer.getValue() != null) {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            return new SimpleStringProperty(sdf.format(customer.getValue().getBirthday()));
                        } else {
                            return new SimpleStringProperty("");
                        }
                    }
                });

        TableColumn<CustomerDto, Long> actionCol = new TableColumn<CustomerDto, Long>("action");
        actionCol.setCellFactory(
                new Callback<TableColumn<CustomerDto,Long>, TableCell<CustomerDto,Long>>() {
                    @Override
                    public TableCell<CustomerDto, Long> call(TableColumn<CustomerDto, Long> arg0) {
                        final TableCell<CustomerDto, Long> cell = new TableCell<CustomerDto, Long>() {
                            @Override
                            protected void updateItem(Long item, boolean empty) {
                                super.updateItem(item, empty);
                                if(empty) {
                                    setText(null);
                                    setGraphic(null);
                                } else {
                                    final Button button = new Button("modifty");
                                    setGraphic(button);
                                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                                }
                            }
                        };

                        return cell;
                    }
                });


        //TODO add button column

        customerTableView.getColumns().addAll(idCol, nameCol, birthdayCol, actionCol);       

        loadData();

    }

    public void loadData() {
        CustomerRepository cusRepo = (CustomerRepository)ContextHolder.getContext().getBean("customerRepository");

        List<Customer> customers = cusRepo.findAll();
        CustomerDtoAssembler customerDTOAssembler = new CustomerDtoAssembler();
        List<CustomerDto> customerDtos = customerDTOAssembler.toDTOList(customers);

        ObservableList<CustomerDto> data = FXCollections.observableArrayList(customerDtos);
        customerTableView.setItems(data);        
    }


    public void showNewDialog(ActionEvent event) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle(
                "com/turbooo/restaurant/interfaces/javafx/customer_holder",
                new UTF8Control());

        Parent container = null;
        try {
            container = FXMLLoader.load(
                    getClass().getResource("customer_holder.fxml"), 
                    resourceBundle);

        } catch (IOException e) {
            e.printStackTrace();
        }

        container.setUserData(this);    //pass the controller, so can access LoadData function in other place later

        Scene scene = new Scene(container, 400, 300);
        Stage stage = new Stage();
        stage.setScene(scene); 
        stage.initModality(Modality.APPLICATION_MODAL);
        stage.show();

    }
}

fxml:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>

<BorderPane prefHeight="345.0" prefWidth="350.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.turbooo.restaurant.interfaces.javafx.CustomerController">
  <center>
    <TableView fx:id="customerTableView" prefHeight="200.0" prefWidth="200.0" />
  </center>
  <top>
    <HBox alignment="CENTER_LEFT" prefHeight="42.0" prefWidth="350.0">
      <children>
        <Button text="new" onAction="#showNewDialog"/>
        <Separator prefHeight="25.0" prefWidth="13.0" visible="false" />
        <Button text="modify" />
        <Separator prefHeight="25.0" prefWidth="15.0" visible="false" />
        <Button text="delete" />
      </children>
    </HBox>
  </top>
</BorderPane>

Upvotes: 2

Views: 930

Answers (1)

jewelsea
jewelsea

Reputation: 159291

You haven't set a cellValueFactory for your action column, so it is always empty. A simple way to do this is just to set the cell value to the id of the record.

actionCol.setCellValueFactory(
    new PropertyValueFactory<CustomerDto,Long>("id")
);

Additionally, I created a simple example for creating a table with Add buttons in it, which you could reference if needed.

Upvotes: 2

Related Questions