2

我有带有可编辑字段 firstName、lastName 和 fullName 的表。我正在像这样创建一个 cellFactory 和 Column

public class SampleFX1 extends Application {
private TableView table = new TableView();
private final ObservableList<Person> data =
        FXCollections.observableArrayList( new Person("balu", "Smith","1"), new Person("Isabella", "john","1"), 
        new Person("Ethan", "Williams","1"), new Person("Emma", "Jones","1"), new Person("Michael", "Brown","1"));

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group());

    TableColumn firstNameCol = createSimpleFirstNameColumn();
    TableColumn lastNameCol = createLastNameColumn();
    TableColumn fullNameCol = createfullNameColumn();
    table.setItems(data);
    table.getColumns().addAll(firstNameCol, lastNameCol,fullNameCol);
    table.setEditable(true);

    ((Group) scene.getRoot()).getChildren().addAll(table);
    stage.setScene(scene);
    stage.show();
}

private TableColumn createSimpleFirstNameColumn() {
    TableColumn firstNameCol = new TableColumn("First Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
    firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
    firstNameCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<Person, String> t) {
            t.getRowValue().setFirstName(t.getNewValue());
        }
    });

    return firstNameCol;
}

private TableColumn createLastNameColumn() {
    Callback<TableColumn, TableCell> editableFactory = new Callback<TableColumn, TableCell>() {
        @Override
        public TableCell call(TableColumn p) {
            return new EditingCell();
        }
    };

    TableColumn lastNameCol = new TableColumn("Last Name");
    lastNameCol.setMinWidth(100);
    lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
    lastNameCol.setCellFactory(editableFactory);
    lastNameCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<Person, String> t) {
          System.out.println( "Commiting last name change. Previous: " + t.getOldValue() + "   New: " + t.getNewValue() );
            t.getRowValue().setLastName(t.getNewValue());
        }
    });

    return lastNameCol;
}   

private TableColumn createfullNameColumn() {
    TableColumn firstNameCol = new TableColumn("full Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("fullName"));
    firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
    firstNameCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
        @Override
        public void handle(TableColumn.CellEditEvent<Person, String> t) {
            t.getRowValue().setfullName(t.getNewValue());
        }
    });

    return firstNameCol;

}
}

编辑单元格:

public class EditingCell extends TableCell<Person, String> {
private TextField textField;

public EditingCell() {
}

@Override
public void startEdit() {
    super.startEdit();

    if( textField == null ) {
        createTextField();
    }
    setText(null);
    setGraphic(textField);
    textField.selectAll();
}

@Override
public void cancelEdit() {
    super.cancelEdit();
    setText((String) getItem());
    setGraphic(null);
}

@Override
public void updateItem(String item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        setText(null);
        setGraphic(null);
    } else {
        if (isEditing()) {
            if (textField != null) {
                textField.setText(getString());
            }
            setText(null);
            setGraphic(textField);
        } else {
            setText(getString());
            setGraphic(null);
        }
    }
}

private void createTextField() {
    textField = new TextField(getString());
    textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
    textField.focusedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) {
            if (!arg2) { commitEdit(textField.getText()); }
        }
    });

    textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent t) {
            if (t.getCode() == KeyCode.ENTER) {
                String value = textField.getText();
                if (value != null) { commitEdit(value); } else { commitEdit(null); }
            } else if (t.getCode() == KeyCode.ESCAPE) {
                cancelEdit();
            }
        }
    });
}

private String getString() {
    return getItem() == null ? "" : getItem().toString();
}
}

人物类:

import javafx.beans.property.SimpleStringProperty;

public class Person {
private  SimpleStringProperty firstName;
private  SimpleStringProperty lastName;
private  SimpleStringProperty fullName;



public Person(String firstName, String lastName ,String fullName) {

    this.firstName = new SimpleStringProperty(firstName);
    this.lastName = new SimpleStringProperty(lastName);
    this.fullName=new SimpleStringProperty(fullName);


}

public String getFirstName() { return firstName.get(); }
public void setFirstName(String firstName) { this.firstName.set(firstName); }
public SimpleStringProperty firstNameProperty() { return firstName; }

public String getLastName() { return lastName.get(); }
public void setLastName(String lastName) { this.lastName.set(lastName); }
public SimpleStringProperty lastNameProperty() { return lastName; }

public String getfullName() { return fullName.get(); }
public void setfullName(String lastName) { this.fullName.set(lastName); }
public SimpleStringProperty fullNameProperty() { return fullName; }

}

问题:当我更新 firstName 列或 Lastname 列(可编辑单元格)而不插入行时,如何更新 fullName 列?

4

2 回答 2

3

使用绑定从名字和姓氏派生全名。

fullName.bind(Bindings.concat(this.firstName, " ", this.lastName));

人.java

import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;

public class Person {
    private SimpleStringProperty firstName;
    private SimpleStringProperty lastName;
    private ReadOnlyStringWrapper fullName = new ReadOnlyStringWrapper();

    public Person(String firstName, String lastName) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName  = new SimpleStringProperty(lastName);

        fullName.bind(Bindings.concat(this.firstName, " ", this.lastName));
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public SimpleStringProperty firstNameProperty() {
        return firstName;
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public SimpleStringProperty lastNameProperty() {
        return lastName;
    }

    public String getFullName() {
        return fullName.get();
    }

    public ReadOnlyStringProperty fullNameProperty() {
        return fullName.getReadOnlyProperty();
    }
}

重新定义您的 createfullNameColumn() 函数。现在 fullName 是连接 firstName 和 lastName 的派生值,因此无需允许用户显式编辑它。

private TableColumn createfullNameColumn() {
  TableColumn fullNameCol = new TableColumn("Full Name");
  fullNameCol.setMinWidth(100);
  fullNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("fullName"));

  return fullNameCol;
}

SampleFX1.java

申请代码:

import javafx.application.Application;
import javafx.collections.*;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.stage.Stage;
import javafx.util.Callback;

public class SampleFX1 extends Application {
    private TableView table = new TableView();
    private final ObservableList<Person> data =
            FXCollections.observableArrayList(
                    new Person("balu", "Smith"),
                    new Person("Isabella", "john"),
                    new Person("Ethan", "Williams"),
                    new Person("Emma", "Jones"),
                    new Person("Michael", "Brown")
            );

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());

        TableColumn firstNameCol = createSimpleFirstNameColumn();
        TableColumn lastNameCol = createLastNameColumn();
        TableColumn fullNameCol = createFullNameColumn();
        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, fullNameCol);
        table.setEditable(true);

        ((Group) scene.getRoot()).getChildren().addAll(table);
        stage.setScene(scene);
        stage.show();
    }

    private TableColumn createSimpleFirstNameColumn() {
        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
        firstNameCol.setCellFactory(TextFieldTableCell.forTableColumn());
        firstNameCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<Person, String> t) {
                t.getRowValue().setFirstName(t.getNewValue());
            }
        });

        return firstNameCol;
    }

    private TableColumn createLastNameColumn() {
        Callback<TableColumn, TableCell> editableFactory = new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn p) {
                return new EditingCell();
            }
        };

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
        lastNameCol.setCellFactory(editableFactory);
        lastNameCol.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<Person, String> t) {
                System.out.println("Commiting last name change. Previous: " + t.getOldValue() + "   New: " + t.getNewValue());
                t.getRowValue().setLastName(t.getNewValue());
            }
        });

        return lastNameCol;
    }

    private TableColumn createFullNameColumn() {
        TableColumn fullNameCol = new TableColumn("Full Name");
        fullNameCol.setMinWidth(100);
        fullNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("fullName"));

        return fullNameCol;
    }
}

编辑单元格.java

(与问题不变)


更新以回答有关示例代码的其他用户问题

即使 eclipse 有很多警告,我也能够运行 createSimpleFirstNameColumn(),例如: TableColumn 是原始类型。应该参数化对泛型类型 TableColumn 的引用。

原始源代码改编自标准JavaFX TableView 示例代码,这在当时并不是使用泛型的最佳示例。Java 中的泛型类型规范是可选的,但是如果你混合使用一个指定泛型的 API,但在 API 使用中没有提供泛型类型信息,编译器会发出如上的警告。一般来说,可以忽略警告,或者如果您愿意,尽管如果您明确指定类型信息,您确实可以获得更好的类型推断和编译时类型检查。

而不是仅仅说:

TableColumn firstNameCol = new TableColumn("First Name");

可以通过明确指定类型信息来消除警告,如下所示:

TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");

我已经更新了上面的示例代码以在createSimpleFirstNameColumn()函数中使用此表示法。

注意上面的代码使用Java 7 菱形表示法,所以程序的最低语言编译级别必须至少设置为 Java 7。

但是,在 createLastNameColumn() 中,带有 return new EditingCell(); 的行给了我一个错误,即 EditingCell 无法解析为一种类型。

我没有收到此错误。您可能没有在项目中包含原始问题中定义的 EditingCell 类。

于 2013-10-02T05:44:04.717 回答
0

我无法发表评论,但要回答这个修订后的问题:

createSimpleFirstNameColumn()即使 eclipse 有很多警告,我也能运行,例如:TableColumn是原始类型。对泛型类型的引用TableColumn应该被参数化。

确保您有正确的导入 import javafx.scene.control.TableColumn;,即没有来自 Java Swing 的任何内容。

于 2015-10-12T14:23:24.180 回答