gpt4 book ai didi

JAVAFX——隐藏栏的神秘解决方案

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

我有一个 TableView,它显示 Derby 表中的所有数据,除了项目的“ID”(我不希望用户看到它。)

方法很简单,我做了一条SQL语句来选择所有属性,但我没有要求ID列。我得到了一个 TableData,这个变量与所有记录一起显示在 TableView 中(它是一个简单的名单)。

我想允许用户使用“删除按钮”从表中删除。

所以首先,当我们收集实际选定行的(如果不为空)ID 时,应该有一个“OnAction”方法(当用户单击删除按钮时),并向数据库发送一条语句来删除它,其中所选项目的(隐藏)ID 可以在 Derby 赛 table 中找到。

由于它是一个名称列表,并且用户可以使用完全相同的数据创建另一条记录,因此在表格 View 中,记录可以是克隆的,没有任何差异。 (只有ID是uniqe,但是tableview不包含id-s,这使得它很难)。

那么我们如何在不知道所选行 ID 的情况下删除它呢?或者说,当表中没有显示ID时,我们如何知道ID呢? (搜索名称不起作用,因为可能有多个具有相同名称的记录)

“隐藏栏的神秘解决方案”是什么? :)

最佳答案

您的问题有一个简单的解决方案。如果您不想在 TableView 上显示 ID,则不必这样做。 TableView在您的情况下与您的类(class)绑定(bind) user ,而不是其字段。将值添加到 TableView 时,您可以控制要在其上显示哪些字段。无论您是否将其显示在表格 View 上,您​​仍然拥有完整的用户对象(仍然具有 ID)。

现在,有多种方法可以做到这一点。其中一种方法是bind the iddelete button您想要在每一行中显示的内容。每当按下删除按钮时,就会从数据库中删除该项目,并从 TableView 中删除它

我创建了一个工作示例来展示其工作原理。我已经对我的值进行了硬编码,而不是从数据库中删除它,而是在控制台上打印 ID 值。

在此示例中,Person类可以被认为相当于你的 user ,每个都有一个 id和其他属性。

import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class TableViewSample extends Application {

private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person(10, "Jacob", "Smith", "jacob.smith@example.com"),
new Person(20, "Isabella", "Johnson", "isabella.johnson@example.com"),
new Person(30, "Ethan", "Williams", "ethan.williams@example.com"),
new Person(40, "Emma", "Jones", "emma.jones@example.com"),
new Person(50, "Michael", "Brown", "michael.brown@example.com")
);

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

@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(600);
stage.setHeight(500);

final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));

table.setEditable(true);

TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));

TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));

TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));


TableColumn deleteCol = new TableColumn("Delete");
deleteCol.setMinWidth(100);
deleteCol.setCellFactory(param -> new ButtonCell());
deleteCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("id"));

table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, deleteCol);

final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);

((Group) scene.getRoot()).getChildren().addAll(vbox);

stage.setScene(scene);
stage.show();
}

public static class Person {

private final SimpleIntegerProperty id;
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;

private Person(Integer id, String fName, String lName, String email) {
this.id = new SimpleIntegerProperty(id);
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}

public int getId() {
return id.get();
}

public void setId(int id) {
this.id.set(id);
}

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

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

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

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

public String getEmail() {
return email.get();
}

public void setEmail(String fName) {
email.set(fName);
}
}

private class ButtonCell extends TableCell<Person, Integer> {
Image buttonDeleteImage = new Image("https://cdn1.iconfinder.com/data/icons/nuove/22x22/actions/fileclose.png");

final Button cellDeleteButton = new Button("", new ImageView(buttonDeleteImage));

ButtonCell() {
cellDeleteButton.setOnAction(actionEvent -> {
System.out.println("Deleted Id : " + getItem());// Make a DB call and delete the person with ID
getTableView().getItems().remove(getIndex());
});
}

@Override
protected void updateItem(Integer t, boolean empty) {
super.updateItem(t, empty);
if (!empty) {
setGraphic(cellDeleteButton);
} else {
setGraphic(null);
}
}
}
}

无需绑定(bind)ID即可实现

您需要:

deleteCol.setCellValueFactory(p -> {
return new ReadOnlyObjectWrapper<Person>((Person)p.getValue());
});

自定义 ButtonCell 应扩展 TableCell<Person, Person>

删除项目的逻辑变为:

System.out.println("Deleted ID : " +
getItem().getId());// Make a DB call and delete the person with ID
getTableView().getItems().remove(getIndex());

完整示例:

public class TableViewSample extends Application {

private TableView<Person> table = new TableView<Person>();
private final ObservableList<Person> data =
FXCollections.observableArrayList(
new Person(10, "Jacob", "Smith", "jacob.smith@example.com"),
new Person(20, "Isabella", "Johnson", "isabella.johnson@example.com"),
new Person(30, "Ethan", "Williams", "ethan.williams@example.com"),
new Person(40, "Emma", "Jones", "emma.jones@example.com"),
new Person(50, "Michael", "Brown", "michael.brown@example.com")
);

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

@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(600);
stage.setHeight(500);

final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));

table.setEditable(true);

TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));

TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));

TableColumn emailCol = new TableColumn("Email");
emailCol.setMinWidth(200);
emailCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("email"));


TableColumn deleteCol = new TableColumn("Delete");
deleteCol.setMinWidth(100);
deleteCol.setCellFactory(param -> new ButtonCell());
deleteCol.setCellValueFactory(p -> {
return new ReadOnlyObjectWrapper<Person>((Person)p.getValue());
});

table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, deleteCol);

final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);

((Group) scene.getRoot()).getChildren().addAll(vbox);

stage.setScene(scene);
stage.show();
}

public static class Person {

private final SimpleIntegerProperty id;
private final SimpleStringProperty firstName;
private final SimpleStringProperty lastName;
private final SimpleStringProperty email;

private Person(Integer id, String fName, String lName, String email) {
this.id = new SimpleIntegerProperty(id);
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
this.email = new SimpleStringProperty(email);
}

public int getId() {
return id.get();
}

public void setId(int id) {
this.id.set(id);
}

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

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

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

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

public String getEmail() {
return email.get();
}

public void setEmail(String fName) {
email.set(fName);
}
}

private class ButtonCell extends TableCell<Person, Person> {
Image buttonDeleteImage = new Image("https://cdn1.iconfinder.com/data/icons/nuove/22x22/actions/fileclose.png");

final Button cellDeleteButton = new Button("", new ImageView(buttonDeleteImage));

ButtonCell() {
cellDeleteButton.setOnAction(actionEvent -> {
System.out.println("Deleted ID : " +
getItem().getId());// Make a DB call and delete the person with ID
getTableView().getItems().remove(getIndex());
});
}

@Override
protected void updateItem(Person t, boolean empty) {
super.updateItem(t, empty);
if (!empty) {
setGraphic(cellDeleteButton);
} else {
setGraphic(null);
}
}
}
}

关于JAVAFX——隐藏栏的神秘解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29054753/

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