我试图在tableview上关注this example。
以下是SSCCE:
summary.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.cell.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<fx:root maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" type="javafx.scene.layout.AnchorPane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<TableView fx:id="table" layoutY="50.0" prefHeight="350.0" prefWidth="600.0">
<columns>
<TableColumn prefWidth="79.5" text="Date">
<cellValueFactory><PropertyValueFactory property="date" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="78" text="Label">
<cellValueFactory><PropertyValueFactory property="label" />
</cellValueFactory>
</TableColumn>
<TableColumn prefWidth="81" text="NumSlices">
<cellValueFactory><PropertyValueFactory property="numSlices" />
</cellValueFactory>
</TableColumn>
</columns>
</TableView>
</children>
</fx:root>
以下是课程:
Summary.java
package sum;
import java.io.IOException;
import java.net.URL;
import java.util.Comparator;
import java.util.ResourceBundle;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.util.Callback;
public class Summary extends AnchorPane implements Initializable {
@FXML
private TableView<SummaryElement> table;
//ObservableList<SummaryElement> data = FXCollections.observableArrayList();
public Summary() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(
"/res/summary.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
@SuppressWarnings("unchecked")
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
ObservableList<SummaryElement> data = table.getItems();
data.add(new SummaryElement("2009/12/12", "T1", 23));
data.add(new SummaryElement("2006/12/12", "T1", 2));
table.getItems().add(new SummaryElement("2011/12/12", "T2",23));
TableColumn<SummaryElement,SummaryElement> btnCol = new TableColumn<>("btnCol");
btnCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SummaryElement,SummaryElement>,ObservableValue<SummaryElement>>(){
@SuppressWarnings("rawtypes")
@Override
public ObservableValue<SummaryElement> call(
javafx.scene.control.TableColumn.CellDataFeatures<SummaryElement, SummaryElement> features) {
return new ReadOnlyObjectWrapper(features.getValue());
}
});
btnCol.setComparator(new Comparator<SummaryElement>() {
@Override public int compare(SummaryElement p1, SummaryElement p2) {
return p1.getLabel().compareTo(p2.getLabel());
}
});
btnCol.setCellFactory(new Callback<TableColumn<SummaryElement, SummaryElement>, TableCell<SummaryElement, SummaryElement>>() {
@Override public TableCell<SummaryElement, SummaryElement> call(TableColumn<SummaryElement, SummaryElement> btnCol) {
return new TableCell<SummaryElement, SummaryElement>() {
final Button button = new Button(); {
button.setMinWidth(130);
}
@Override public void updateItem(final SummaryElement SummaryElement, boolean empty) {
super.updateItem(SummaryElement, empty);
if (SummaryElement != null) {
button.setText("Buy coffee" + SummaryElement.getLabel());
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent event) {
// actionTaken.setText("Bought " + SummaryElement.getLikes().toLowerCase() + " for: " + SummaryElement.getFirstName() + " " + SummaryElement.getLastName());
}
});
} else {
setGraphic(null);
}
}
};
}
});
table.getColumns().add(btnCol);
}
}
要调用应用程序的类:
package sum;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class CustomDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
Summary custom = new Summary();
Scene scene = new Scene(custom);
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
摘要元素就是这样:
package sum;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class SummaryElement {
private final SimpleStringProperty date = new SimpleStringProperty("");
private final SimpleStringProperty label = new SimpleStringProperty("");
private final IntegerProperty numSlices = new SimpleIntegerProperty(this, "numSlices", 0);
public SummaryElement(String date, String label, int numSlices){
setDate(date);
setLabel(label);
setNumSlices(numSlices);
}
public void setDate(String gDate){
date.set(gDate);
}
public String getDate(){
return date.get();
}
public void setLabel(String gLabel){
label.set(gLabel);
}
public String getLabel(){
return label.getValue();
}
public void setNumSlices(int gNumSlices){
numSlices.set(gNumSlices);
}
public int getNumSlices(){
return numSlices.get();
}
}
当我在eclipse中进行调试时,在Summary.java中的第77行有一个断点我可以在Variables窗口中看到它加载了SummaryElement但是当我点击进入下一步时它会显示一个错误消息:“找不到来源”。
然后它会在btnCol上显示没有任何按钮的tableview,即使它很难进入if (SummaryElement != null)
几次。
我很抱歉,如果这篇文章看起来像一个帮助吸血鬼,但我尝试了3天没有成功。
答案 0 :(得分:2)
在TableCell
的{{1}}方法的updateItem(...)
子句中,您需要if (SummaryElement != null)