我在令牌“>>”上遇到语法错误,在此令牌之后预期表达式,但在下面调用setCellFactory
但不明白原因。
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TableVieww extends Application {
private static TableView<TableData> table;
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unchecked")
public void start(Stage primaryStage) throws Exception {
Stage s = primaryStage;
s.setTitle("The Title");
TableColumn<TableData, String> column1 = new TableColumn<>("String");
column1.setMinWidth(150);
column1.setCellValueFactory(new PropertyValueFactory<TableData, String>("s"));
column1.setEditable(true);
column1.setCellFactory(TextFieldTableCell.forTableColumn());
column1.setOnEditCommit(e -> {
});
TableColumn<TableData, Double> column2 = new TableColumn<>("Double");
column2.setMinWidth(50);
column2.setCellValueFactory(new PropertyValueFactory<TableData, Double>("d"));
column2.setEditable(true);
// Error within this line (The line below)
column2.setCellFactory(TextFieldTableCell.forTableColumn());
column2.setOnEditCommit(e -> {
});
// set up a table
table = new TableView<>();
table.setItems(getData());
table.getColumns().addAll(column1, column2);
table.setEditable(true);
// setting up a layout
VBox layout = new VBox(10);
layout.getChildren().add(table);
// Setting up a scene
Scene scene = new Scene(layout, 500, 200);
s.setScene(scene);
s.show();
}
private static ObservableList<TableData> getData(){
ObservableList<TableData> list = FXCollections.observableArrayList();
TableVieww view = new TableVieww();
list.add(view.new TableData("BlaBlaBla", 10));
list.add(view.new TableData("TraTraTra", 5.1));
return list;
}
public class TableData{
double d;
String s;
public TableData(String s, double d) {
this.d = d;
this.s = s;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
}
}
有什么问题?感谢您的帮助!
答案 0 :(得分:1)
这并非完全无瑕,但它确实有效。你可能需要调整它太多适合你的需求,但......它的工作原理。
public class SpinnerTableCell<S, T> extends TableCell<S, T> {
private Spinner spinner;
private String text;
public SpinnerTableCell(int min, int max) {
this.spinner = new Spinner(min, max, 0.0);
spinner.focusedProperty().addListener((observable, oldBoolean, newBoolean)->{
if(!newBoolean){
setText(spinner.getValue().toString());
text = getText();
setGraphic(null);
}
});
}
public void startEdit() {
if (this.isEditable() && this.getTableView().isEditable() && this.getTableColumn().isEditable()) {
super.startEdit();
if (this.isEditing()) {
setText("");
setGraphic(spinner);
}
}
}
public void cancelEdit() {
super.cancelEdit();
setText(text);
setGraphic(null);
}
public void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : item.toString());
spinner.getValueFactory().setValue(empty ? 0.0 : item);
}
}
您可以使用tableCell.setCellFactory((e)->new SpinnerTableCell(min, max));