我使用 javafx2 控件 TableView 来显示数据库中的记录(我现在有 20 条记录)。当显示记录时,我想在每一行中添加按钮。所以我用谷歌搜索了很多文章,并在下面写了一些代码。
关键代码是:
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);
}
}
我调试这段代码,参数“empty”永远不会是“false”,所以按钮永远不会显示在表格视图中,
谁能帮我?谢谢
下面是完整的代码(java 和 fxml):
爪哇:
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();
}
}
文件格式:
<?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>