gpt4 book ai didi

java - 基于 JavaFX 中 TableRow 中对象属性的自定义单元格操作

转载 作者:行者123 更新时间:2023-12-02 03:27:12 25 4
gpt4 key购买 nike

大家。

我有一个像这样的 javaFx tableView :

-----------------------------------------| Name       |  Date       | actions    |-----------------------------------------| jack       |23-06-2016   | B1  B2  B3 |-----------------------------------------| Jane       |12-02-2017   | B1  B2  B3 |-----------------------------------------| Mickel     |22-05-2017   | B1  B2  B3 |-----------------------------------------

操作列使用 CustomCellTable 呈现。它包含三个按钮(B1、B2 和 B3)

我希望仅当相应日期是表中的最大值时才启用 B3 按钮。

在上面的示例中,最后一行的 B3 按钮应该启用,其他按钮不应该启用

预先感谢您的帮助。

最佳答案

假设您有一个如下所示的模型类

public class Person {

// ...

public StringProperty nameProperty() {
return name ;
}

public ObjetProperty<LocalDate> dateProperty() {
return date ;
}

public LocalDate getDate() {
return dateProperty().get();
}

// ...
}

如果您使用extractor为表创建项目列表:

ObservableList<Person> tableData = FXCollections.observableArrayList(p -> new Observable[] {p.dateProperty()});
tableData.addAll(...);
table.setItems(tableData);

那么你可以这样做

ObjectBinding<Person> personWithLatestDate = Bindings.createObjectBinding(() -> 
tableData.stream().max(Comparator.comparing(Person::getDate)).orElse(null),
tableData);

您的操作列可能如下所示:

TableColumn<Person, Person> actionsColumn = new TableColumn<>("Actions");
actionsColumn.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue()));

actionsColumn.setCellFactory(tc -> new TableCell<Person, Person>() {
// ...
Button b3 = new Button(...) ;
HBox buttons = new HBox(5, b1, b2, b3);
// ...

{
b3.disableProperty().bind(itemProperty().isNotEqualTo(personWithLatestDate));
}

@Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setGraphic(empty ? null : buttons);
}
});

这是 SSCCE:

import java.time.LocalDate;
import java.util.Comparator;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;

public class TableCelDisableButton extends Application {

private ObservableList<Person> tableData;

@Override
public void start(Stage primaryStage) {

TableView<Person> table = new TableView<>();
tableData = FXCollections.observableArrayList(p -> new Observable[] {p.dateProperty()});
table.setItems(tableData);

ObjectBinding<Person> personWithLatestDate = Bindings.createObjectBinding(() ->
tableData.stream().max(Comparator.comparing(Person::getDate)).orElse(null),
tableData);

TableColumn<Person, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(cellData -> cellData.getValue().nameProperty());

TableColumn<Person, LocalDate> dateCol = new TableColumn<>("Date");
dateCol.setCellValueFactory(cellData -> cellData.getValue().dateProperty());

TableColumn<Person, Person> actionsCol = new TableColumn<>("Actions");
actionsCol.setCellValueFactory(cellData -> new SimpleObjectProperty<>(cellData.getValue()));

actionsCol.setCellFactory(tc -> new TableCell<Person, Person>() {
Button edit = new Button("Edit");
Button button = new Button("Click");
HBox buttons = new HBox(5, edit, button);

{
edit.setOnAction(e -> edit(getItem(), primaryStage, false));
button.disableProperty().bind(itemProperty().isNotEqualTo(personWithLatestDate));
}

@Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setGraphic(empty ? null : buttons);
}
});

table.getColumns().add(nameCol);
table.getColumns().add(dateCol);
table.getColumns().add(actionsCol);

table.getItems().add(new Person("Jack", LocalDate.of(2016, 6, 23)));
table.getItems().add(new Person("Jane", LocalDate.of(2017, 2, 12)));
table.getItems().add(new Person("Mikel", LocalDate.of(2017, 5, 22)));

Button add = new Button("Add");
add.setOnAction(e -> edit(new Person("", LocalDate.now()), primaryStage, true));

Button delete = new Button("Delete");
delete.disableProperty().bind(table.getSelectionModel().selectedItemProperty().isNull());
delete.setOnAction(e -> tableData.remove(table.getSelectionModel().getSelectedIndex()));

HBox buttons = new HBox(5, add, delete);
buttons.setPadding(new Insets(5));
buttons.setAlignment(Pos.CENTER);


Scene scene = new Scene(new BorderPane(table, null, null, buttons, null), 600, 600);
primaryStage.setScene(scene);
primaryStage.show();
}

private void edit(Person person, Window primaryStage, boolean addOnCommit) {
GridPane editor = new GridPane();
ColumnConstraints leftCol = new ColumnConstraints();
leftCol.setHgrow(Priority.NEVER);
leftCol.setHalignment(HPos.RIGHT);
ColumnConstraints rightCol = new ColumnConstraints();
rightCol.setHgrow(Priority.SOMETIMES);
rightCol.setHalignment(HPos.LEFT);
editor.getColumnConstraints().addAll(leftCol, rightCol);

editor.setHgap(5);
editor.setVgap(8);
editor.setPadding(new Insets(10));

editor.add(new Label("Name:"), 0, 0);
editor.add(new Label("Date"), 0, 1);
TextField name = new TextField(person.getName());
editor.add(name, 1, 0);
DatePicker date = new DatePicker(person.getDate());
editor.add(date, 1, 1);

Button ok = new Button("OK");
Button cancel = new Button("Cancel");
HBox buttons = new HBox(5, ok, cancel);
buttons.setAlignment(Pos.CENTER);
editor.add(buttons, 0, 2, 2, 1);


Scene scene = new Scene(editor);
Stage editDialog = new Stage();
editDialog.setScene(scene);
editDialog.initModality(Modality.APPLICATION_MODAL);
editDialog.initOwner(primaryStage);
editDialog.initStyle(StageStyle.UNDECORATED);

editDialog.sizeToScene();

ok.setOnAction(e -> {
person.setName(name.getText());
person.setDate(date.getValue());
if (addOnCommit) {
tableData.add(person);
}
editDialog.hide();
});

cancel.setOnAction(e -> editDialog.hide());

editDialog.show();
}

public static class Person {
private final StringProperty name = new SimpleStringProperty();
private final ObjectProperty<LocalDate> date = new SimpleObjectProperty<>();

public Person(String name, LocalDate date) {
setName(name);
setDate(date);
}

public final StringProperty nameProperty() {
return this.name;
}


public final java.lang.String getName() {
return this.nameProperty().get();
}


public final void setName(final java.lang.String name) {
this.nameProperty().set(name);
}


public final ObjectProperty<LocalDate> dateProperty() {
return this.date;
}


public final java.time.LocalDate getDate() {
return this.dateProperty().get();
}


public final void setDate(final java.time.LocalDate date) {
this.dateProperty().set(date);
}



}

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

关于java - 基于 JavaFX 中 TableRow 中对象属性的自定义单元格操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38680166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com