【问题标题】:Syntax error on token ">>", Expression expected after this token标记“>>”上的语法错误,此标记后应为表达式
【发布时间】:2015-07-20 21:36:09
【问题描述】:

当我在下面调用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;
        }


    }



}

怎么了?提前感谢您的帮助!

【问题讨论】:

  • 清理以提高清晰度和可读性
  • 您的代码不可执行,您正在使用我没有的类,试试看能否将问题缩小到我可以运行的小类。

标签: javafx cell tablecolumn


【解决方案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)-&gt;new SpinnerTableCell(min, max));设置工厂

【讨论】:

  • 没有修复错误,实际上出现了新错误:令牌“,”的语法错误,删除此令牌
  • 这样,eclipse 说我需要再添加一个 TableCell 的方法,这不是问题,但是我有这两种方法,它们需要返回一些东西,所以我应该把回报?难道没有更简单的方法来做这些事情吗?对于 String 列,它适用于 stateColumn.setCellFactory(TextFieldTableCell.forTableColumn());
  • 使用Eclipse生成的那个,我的肯定是不正确的。
  • 你想做什么?如果您只是显示Double,那么我认为您甚至不需要TableColumn.setCellFactory() 调用。
  • 试试TableColumn.setEditable(true)
猜你喜欢
  • 2017-02-21
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多