Note that there are some explanatory texts on larger screens.

plurals
  1. POupdateItem's second param have never be false in javafx2
    primarykey
    data
    text
    <p>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.</p> <p>the key code is:</p> <pre><code> 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); } } </code></pre> <p>I debug this code,the param "empty" would never be "false", so the button would never be display in the table view,</p> <p>can anyone help me? thanks</p> <p>below is the complete code(java and fxml):</p> <p>java:</p> <pre><code> 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&lt;CustomerDto&gt; customerTableView; @Override public void initialize(URL arg0, ResourceBundle arg1) { TableColumn&lt;CustomerDto, Long&gt; idCol = new TableColumn&lt;CustomerDto, Long&gt;("id"); idCol.setCellValueFactory( new PropertyValueFactory&lt;CustomerDto,Long&gt;("id") ); TableColumn&lt;CustomerDto, String&gt; nameCol = new TableColumn&lt;CustomerDto, String&gt;("name"); nameCol.setMinWidth(100); nameCol.setCellValueFactory( new PropertyValueFactory&lt;CustomerDto,String&gt;("name") ); TableColumn&lt;CustomerDto, String&gt; birthdayCol = new TableColumn&lt;CustomerDto, String&gt;("birthday"); birthdayCol.setCellValueFactory( new Callback&lt;TableColumn.CellDataFeatures&lt;CustomerDto, String&gt;, ObservableValue&lt;String&gt;&gt;() { @Override public ObservableValue&lt;String&gt; call(TableColumn.CellDataFeatures&lt;CustomerDto, String&gt; 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&lt;CustomerDto, Long&gt; actionCol = new TableColumn&lt;CustomerDto, Long&gt;("action"); actionCol.setCellFactory( new Callback&lt;TableColumn&lt;CustomerDto,Long&gt;, TableCell&lt;CustomerDto,Long&gt;&gt;() { @Override public TableCell&lt;CustomerDto, Long&gt; call(TableColumn&lt;CustomerDto, Long&gt; arg0) { final TableCell&lt;CustomerDto, Long&gt; cell = new TableCell&lt;CustomerDto, Long&gt;() { @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&lt;Customer&gt; customers = cusRepo.findAll(); CustomerDtoAssembler customerDTOAssembler = new CustomerDtoAssembler(); List&lt;CustomerDto&gt; customerDtos = customerDTOAssembler.toDTOList(customers); ObservableList&lt;CustomerDto&gt; 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(); } } </code></pre> <p>fxml:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?import java.lang.*?&gt; &lt;?import java.util.*?&gt; &lt;?import javafx.scene.control.*?&gt; &lt;?import javafx.scene.layout.*?&gt; &lt;?import javafx.scene.paint.*?&gt; &lt;BorderPane prefHeight="345.0" prefWidth="350.0" xmlns:fx="http://javafx.com/fxml" fx:controller="com.turbooo.restaurant.interfaces.javafx.CustomerController"&gt; &lt;center&gt; &lt;TableView fx:id="customerTableView" prefHeight="200.0" prefWidth="200.0" /&gt; &lt;/center&gt; &lt;top&gt; &lt;HBox alignment="CENTER_LEFT" prefHeight="42.0" prefWidth="350.0"&gt; &lt;children&gt; &lt;Button text="new" onAction="#showNewDialog"/&gt; &lt;Separator prefHeight="25.0" prefWidth="13.0" visible="false" /&gt; &lt;Button text="modify" /&gt; &lt;Separator prefHeight="25.0" prefWidth="15.0" visible="false" /&gt; &lt;Button text="delete" /&gt; &lt;/children&gt; &lt;/HBox&gt; &lt;/top&gt; &lt;/BorderPane&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload