我有 TableView,当我的程序启动 TableView 只有一行时,我还有 ComboBoxes 的 ArrayList 并为 TableView 中的每一行创建一个 ComboBox,当用户编辑(产品名称)单元格(其中应包含 ComboBox)程序时创建此单元格的组合框并将组合框设置为单元格图形(当创建组合框时未从单元格图形中删除),并且当用户从最后一行组合框选择项目时,程序必须创建一行,程序对于第一行组合框操作行为正确但第二行失败了,请注意最后一个组合框仅其操作创建了一个附加行,而不是每个组合框,这张照片可以帮助说明我的问题
这是我的代码:
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BillDesign extends Application
{
private final TableView <Products> tableProducts = new TableView();
private final TableColumn serial = new TableColumn("Serial");
private final TableColumn productName = new TableColumn("ProductName");
private final ObservableList <Products> data = FXCollections.observableArrayList(new Products("1", ""),
new Products("2", ""),
new Products("3", ""),
new Products("4", ""));
private final Button button = new Button("Click");
@Override
public void start(Stage stage)
{
serial.setEditable(true);
serial.setCellValueFactory(new PropertyValueFactory("Serial"));
productName.setEditable(true);
productName.setCellValueFactory(new PropertyValueFactory("ProductName"));
productName.setCellFactory(column -> new TableCell ()
{
private ComboBox comboBox;
@Override
public void startEdit()
{
if(!isEmpty())
{
super.startEdit();
comboBox = new ComboBox();
comboBox.setPrefWidth(getWidth());
comboBox.setEditable(true);
comboBox.getItems().addAll("A", "B", "C");
setGraphic(comboBox);
comboBox.setOnAction(e ->
{
data.get(getIndex()).setProductName(comboBox.getSelectionModel().getSelectedItem().
toString());
if(getIndex() == data.size() - 1)
{
data.add(new Products(String.valueOf(data.size() + 1), ""));
}
});
}
}
});
button.setOnMouseClicked(e ->
{
for(int i = 0; i < data.size(); i++)
{
System.out.println("Name " + data.get(i).getProductName() + "\tSerial " + data.get(i).getSerial());
}
});
tableProducts.setEditable(true);
tableProducts.getColumns().addAll(serial, productName);
tableProducts.setItems(data);
VBox root = new VBox(tableProducts, button);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public class Products
{
private final SimpleStringProperty Serial;
private final SimpleStringProperty ProductName;
public Products(String serial, String productName)
{
this.Serial = new SimpleStringProperty(serial);
this.ProductName = new SimpleStringProperty(productName);
}
public void setSerial(String serial)
{
Serial.set(serial);
}
public String getSerial()
{
return Serial.get();
}
public void setProductName(String productName)
{
ProductName.set(productName);
}
public String getProductName()
{
return ProductName.get();
}
}
}
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)