Javafx可编辑表格单元格

时间:2013-10-02 05:13:17

标签: javafx

我有可编辑字段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列或姓氏列(可编辑单元格)而不插入行时,如何更新fullName列?

2 个答案:

答案 0 :(得分:3)

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

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

Person.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;
    }
}

EditingCell.java

(与问题无关)


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

  

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

原始源代码是根据标准JavaFX TableView sample code改编的,当时这不是使用Generics的最佳示例。 Java中的泛型类型规范是可选的,但如果混合使用指定了泛型的API,但不在API使用中提供泛型类型信息,则编译器将发出如上所述的警告。一般来说,警告可以被忽略,或者如果你愿意,虽然如果明确指定类型信息,你可以获得更好的类型推断和编译时间类型检查。

而不仅仅是说:

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

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

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

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

请注意,上面的代码使用Java 7 diamond notation,因此程序的最低语言编译级别必须至少设置为Java 7.

  

但是,在createLastNameColumn()中,返回new EditingCell()的行给出了一个错误,即EditingCell无法解析为某个类型。

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

答案 1 :(得分:0)

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

即使有很多来自eclipse的警告,我也可以运行createSimpleFirstNameColumn(),例如:TableColumn是原始类型。应该参考通用类型TableColumn的参考。

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