Javafx ComboBoxTableCell,回调
我有一个我无论如何也解决不了的问题。我已经阅读了我在网上找到的所有东西,并尝试了几十种代码,但我仍然没有解决方案 它是一个遵循MVC模型的JavaFX项目。fxml文件如下所示: 主要类别:Javafx ComboBoxTableCell,回调,javafx,combobox,tablecell,Javafx,Combobox,Tablecell,我有一个我无论如何也解决不了的问题。我已经阅读了我在网上找到的所有东西,并尝试了几十种代码,但我仍然没有解决方案 它是一个遵循MVC模型的JavaFX项目。fxml文件如下所示: 主要类别: public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Sta
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
AnchorPane root = null;
try {
root = (AnchorPane) FXMLLoader.load(getClass().getResource("sample.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
fxml文件的控制器是:
public class Controller {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TableView<Fila> taula;
@FXML
private TableColumn<Fila, String> c1;
@FXML
private TableColumn<Fila, ObservableList<String>> c2;
@FXML
void initialize() {
assert taula != null : "fx:id=\"taula\" was not injected: check your FXML file 'sample.fxml'.";
assert c1 != null : "fx:id=\"c1\" was not injected: check your FXML file 'sample.fxml'.";
assert c2 != null : "fx:id=\"c2\" was not injected: check your FXML file 'sample.fxml'.";
ObservableList<Fila> dades = FXCollections.observableArrayList(
new Fila("First", FXCollections.observableArrayList("1", "2", "3")),
new Fila("Second", FXCollections.observableArrayList("4", "5", "6")),
new Fila("Third", FXCollections.observableArrayList("7", "8", "9"))
);
c1.setCellValueFactory(cellData -> cellData.getValue().c1Property());
c2.setCellValueFactory(cellData -> cellData.getValue().c2Property());
taula.setItems(dades);
}
}
公共类控制器{
@FXML
私人资源;
@FXML
私有URL位置;
@FXML
私人TableView taula;
@FXML
私人表格c1栏;
@FXML
私人表格c2栏;
@FXML
void initialize(){
assert taula!=null:“fx:id=\“taula\”未被注入:检查FXML文件'sample.FXML';
断言c1!=null:“fx:id=\“c1\”未被注入:检查FXML文件“sample.FXML”;
断言c2!=null:“fx:id=\“c2\”未被注入:检查FXML文件“sample.FXML”;
ObservableList dades=FXCollections.observableArrayList(
新Fila(“第一”,FXCollections.observableArrayList(“1”、“2”、“3”)),
新Fila(“第二”,FXCollections.observableArrayList(“4”、“5”、“6”)),
新Fila(“第三”,FXCollections.observableArrayList(“7”、“8”、“9”))
);
c1.setCellValueFactory(cellData->cellData.getValue().c1Property());
c2.setCellValueFactory(cellData->cellData.getValue().c2Property());
taula.setItems(dades);
}
}
bean类是:
public class Fila {
private StringProperty c1;
private ListProperty<String> c2;
public Fila(String c1, ObservableList<String> c2) {
this.c1 = new SimpleStringProperty(c1);
this.c2 = new SimpleListProperty<>(c2);
}
public String getC1() {
return c1.get();
}
public StringProperty c1Property() {
return c1;
}
public void setC1(String c1) {
this.c1.set(c1);
}
public ObservableList<String> getC2() {
return c2.get();
}
public ListProperty<String> c2Property() {
return c2;
}
public void setC2(ObservableList<String> c2) {
this.c2.set(c2);
}
}
公共类Fila{
私有财产c1;
私有财产c2;
公共Fila(字符串c1,可观察列表c2){
this.c1=新的SimpleStringProperty(c1);
this.c2=新的简单属性(c2);
}
公共字符串getC1(){
返回c1.get();
}
公共字符串属性c1Property(){
返回c1;
}
公共无效集合c1(字符串c1){
本.c1.集(c1);
}
公共观察列表getC2(){
返回c2.get();
}
公共列表属性c2Property(){
返回c2;
}
公共无效集合c2(可观察列表c2){
本.c2.set(c2);
}
}
我没有得到的是,在第二列中出现了一个带有值的组合框现在显示为列表
我认为可以使用setCellFactory
方法以及回调
来实现,因为我们需要组合框
,但我无法获得它
对不起我的英语水平;很明显,我正在使用一个翻译…你可以这样做
c2.setCellFactory(column -> new TableCell<Fila, ObservableList<String>>() {
private final ComboBox<String> combo = new ComboBox<>();
{
combo.valueProperty().addListener((obs, oldValue, newValue) -> {
// in real life, update model appropriately here...
System.out.println("Selected "+newValue+" for "+getTableView().getItems().get(getIndex()).getC1());
});
}
@Override
protected void updateItem(ObservableList<String> items, boolean empty) {
super.updateItem(items, empty);
if (empty) {
setGraphic(null);
} else {
combo.setItems(items);
// in real life, do combo.setValue(...) with some value from model
setGraphic(combo);
}
}
});
c2.setCellFactory(列->新表单元格(){
私有最终组合框combo=新组合框();
{
combo.valueProperty().addListener((obs、oldValue、newValue)->{
//在现实生活中,在这里适当地更新模型。。。
System.out.println(“为“+getTableView().getItems().get(getIndex()).getC1()”选择“+newValue+”;
});
}
@凌驾
受保护的void updateItem(可观察列表项,布尔值为空){
super.updateItem(项,空);
if(空){
设置图形(空);
}否则{
combo.setItems(项目);
//在现实生活中,使用模型中的一些值来组合.setValue(…)
设置图形(组合);
}
}
});
您的模型没有为组合框提供足够的信息,因为它没有任何表示组合框中哪个项被选中的属性。大概你只是没有在你发布的例子中添加这个;组合框的
valueProperty()
上的侦听器应该适当地更新它,而updateItem()
方法应该根据适当的数据设置组合框的值。非常感谢您,James\u D。这是一个简单、优雅的解决方案,而且工作非常完美。