[英]JavaFX Table Cell Editing
我正在嘗試用 Java 制作一個程序來管理我的博彩賬戶。 我是 Java 新手,所以我想我會選擇一些簡單的東西來看看事情是如何工作的。 我決定使用 tableview 並使單個單元格可編輯。 我一直在關注本教程http://java-buddy.blogspot.co.uk/2012/04/javafx-2-editable-tableview.html 。 它詳細說明了如何使用 java 代碼來完成它,並將其復制到一個新類中可以完美地工作。 我決定嘗試調整它以使用 FXML,因為我喜歡 Sceneviewer。 我的問題是,數據已加載到表格中,但是當我單擊/雙擊單元格時,沒有任何反應。 這是我的代碼。
測試控制器.java
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Callback;
import java.net.URL;
import java.util.ResourceBundle;
public class testController implements Initializable {
@FXML
private TableColumn<Account, String> usernameCol;
@FXML
private TableColumn<Account, String> balanceCol;
@FXML
private TableView<Account> accountTable;
@FXML
private TableColumn<Account, String> bookieCol;
@FXML
private TableColumn<Account, String> passwordCol;
private ObservableList<Account> dataList =
FXCollections.observableArrayList(
new Account("bookie", "username", "password", "0"));
@Override
public void initialize(URL location, ResourceBundle resources) {
Callback<TableColumn, TableCell> cellFactory =
new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
return new EditingCell();
}
};
bookieCol.setCellValueFactory(
new PropertyValueFactory<>("fieldBookie"));
usernameCol.setCellValueFactory(
new PropertyValueFactory<>("fieldUsername"));
usernameCol.setOnEditCommit(
new EventHandler<TableColumn.CellEditEvent<Account, String>>() {
@Override public void handle(TableColumn.CellEditEvent<Account, String> t) {
((Account)t.getTableView().getItems().get(
t.getTablePosition().getRow())).setFieldUsername(t.getNewValue());
}
});
passwordCol.setCellValueFactory(
new PropertyValueFactory<Account, String>("fieldPassword"));
balanceCol.setCellValueFactory(
new PropertyValueFactory<Account, String>("fieldBalance"));
accountTable.setItems(dataList);
}
class EditingCell extends TableCell<Account, String> {
private TextField textField;
public EditingCell() {
}
@Override
public void startEdit() {
super.startEdit();
if (textField == null) {
createTextField();
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
textField.selectAll();
}
@Override
public void cancelEdit() {
super.cancelEdit();
setText(String.valueOf(getItem()));
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(textField);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setGraphic(textField);
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
} else {
setText(getString());
setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
}
private void createTextField() {
textField = new TextField(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
textField.setOnKeyPressed(t -> {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(textField.getText());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
});
}
private String getString() {
return getItem() == null ? "" : getItem();
}
}
}
這是我的 Account.java 文件。
import javafx.beans.property.SimpleStringProperty;
public class Account {
private SimpleStringProperty fieldBookie;
private SimpleStringProperty fieldUsername;
private SimpleStringProperty fieldPassword;
private SimpleStringProperty fieldBalance;
Account(String fbookie, String fusername, String fpassword, String fbalance){
this.fieldBookie = new SimpleStringProperty(fbookie);
this.fieldUsername = new SimpleStringProperty(fusername);
this.fieldPassword = new SimpleStringProperty(fpassword);
this.fieldBalance = new SimpleStringProperty(fbalance);
}
public String getFieldBookie() {
return fieldBookie.get();
}
public String getFieldUsername() {
return fieldUsername.get();
}
public String getFieldPassword() {
return fieldPassword.get();
}
public String getFieldBalance() {
return fieldBalance.get();
}
public void setFieldBookie(String fBookie) {
fieldBookie.set(fBookie);
}
public void setFieldUsername(String fUsername) {
fieldUsername.set(fUsername);
}
public void setFieldPassword(String fPassword) {
fieldUsername.set(fPassword);
}
public void setFieldBalance(String fBalance) {
fieldUsername.set(fBalance);
}
}
最后,這是我的 fxml 文件。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.canvas.*?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<Group xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testController">
<children>
<TableView fx:id="accountTable" editable="true" prefHeight="291.0" prefWidth="302.0">
<columns>
<TableColumn fx:id="bookieCol" prefWidth="75.0" text="Bookie" />
<TableColumn fx:id="usernameCol" prefWidth="75.0" text="Username" />
<TableColumn fx:id="passwordCol" prefWidth="75.0" text="Password" />
<TableColumn fx:id="balanceCol" prefWidth="75.0" text="Balance" />
</columns>
</TableView>
</children>
</Group>
正如我之前所說,當我單擊用戶名單元格時沒有任何反應。 沒有錯誤,沒有文本字段,什么都沒有。 任何幫助將不勝感激! 謝謝
我沒有試過你的例子,但我認為你只是忘記為特定列設置 cellFactory 。 添加以下行應該修復它:
usernameCol.setCellFactory(cellFactory);
如果有人需要一個工作示例,我可以通過添加一個代碼來使代碼與本教程一起使用
usernameCol.setCellFactory(
TextFieldTableCell.forTableColumn());
並將usernameCol.setOnEditCommit
更改為
usernameCol.setOnEditCommit(
(TableColumn.CellEditEvent<Account, String> t) ->
( t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setFieldUsername(t.getNewValue())
);
這是應該工作的完整testController
類(其他文件保持不變)
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import java.net.URL;
import java.util.ResourceBundle;
public class testController implements Initializable {
@FXML
private TableColumn<Account, String> usernameCol;
@FXML
private TableColumn<Account, String> balanceCol;
@FXML
private TableView<Account> accountTable;
@FXML
private TableColumn<Account, String> bookieCol;
@FXML
private TableColumn<Account, String> passwordCol;
private ObservableList<Account> dataList =
FXCollections.observableArrayList(
new Account("bookie", "username", "password", "0"));
@Override
public void initialize(URL location, ResourceBundle resources) {
bookieCol.setCellValueFactory(
new PropertyValueFactory<>("fieldBookie"));
usernameCol.setCellValueFactory(
new PropertyValueFactory<>("fieldUsername"));
usernameCol.setCellFactory(
TextFieldTableCell.forTableColumn());
usernameCol.setOnEditCommit(
(TableColumn.CellEditEvent<Account, String> t) ->
( t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setFieldUsername(t.getNewValue())
);
passwordCol.setCellValueFactory(
new PropertyValueFactory<Account, String>("fieldPassword"));
balanceCol.setCellValueFactory(
new PropertyValueFactory<Account, String>("fieldBalance"));
accountTable.setItems(dataList);
}
}
您可以使用以下方法將 textField 添加到編輯單元格: TextFieldTableCell.forTableColumn();
如果你需要ComboBoxTableCell.forTableColumn(list);
試試: ComboBoxTableCell.forTableColumn(list);
// TextField:
usernameCol.setCellFactory(TextFieldTableCell.forTableColumn());
// ComboBox:
ObservableList<String> list = FXCollections.observableArrayList();
list.add("name 1");
list.add("name 2");
list.add("name 3");
list.add("name 4");
Callback<TableColumn<Account, String>, TableCell<Account, String>> cbtc =
ComboBoxTableCell.forTableColumn(list);
usernameCol.setCellFactory(cbtc);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.