在 TableView JavaFX 中设置 ComboBoxCell

Set ComboBoxCell in TableView JavaFX

我按照这个问题的说明在我的应用程序的 TableView 中设置了一个 ComboBoxCell。 (How to put ComboBoxTableCell in a TableView?)

Cell 的声明工作正常,但 comboBox 没有出现在 table 中。我认为是这样的,因为 只有 col1 和 col2 在我的模型中。 连接数据库后我的table-entry里面没有写col3.

我不知道如何在 TableView 中创建组合框,需要您的帮助。

这是我的代码:

控制器:

package controller;

imports

public class main_controller implements Initializable {
    private ObservableList<model> tableData = FXCollections.observableArrayList();
    private ObservableList<String> cbValues = FXCollections.observableArrayList("1", "2", "3");

    @FXML
    private TableView<model> ComboTable;
    @FXML
    private TableColumn<model, String> col1;
    @FXML
    private TableColumn<model, String> col2;
    @FXML
    private TableColumn<model, String> col3;

    public main_controller() {

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        tableData.clear();
        col1.setCellValueFactory(new PropertyValueFactory<model, String>("rCol1"));
        col2.setCellValueFactory(new PropertyValueFactory<model, String>("rCol2"));
        col3.setCellFactory(ComboBoxTableCell.forTableColumn(new DefaultStringConverter(), cbValues));

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            System.out.println("*** Loaded Oracle-Driver ***");
        } catch (ClassNotFoundException e1) {
            System.out.println("Driver-Loading failed.");
            e1.printStackTrace();
        }
        try {
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:lukas/1234@10.140.79.56:1521:OTTO");
            Statement statement = conn.createStatement();
            ResultSet resultset = statement.executeQuery("SELECT NUMMER, DATEN1, DATEN2 FROM LUKAS order by NUMMER");
            String Daten1 = "empty";
            String Daten2 = "empty";
            // Zum einfügen kann man später auf diese Variable zurückgreifen.
            int columnIndex;
            System.out.println("*** Connected with Database ***");
            while (resultset.next()) {
                columnIndex = resultset.getInt("NUMMER");
                System.out.println("Tabellenindex der Zeile:\t" + columnIndex);
                Daten1 = resultset.getString("DATEN1");
                Daten2 = resultset.getString("DATEN2");
                System.out.println("Daten1:\t " + Daten1 + "\t\t\tDaten2: " + Daten2);
                **model entry = new model(Daten1, Daten2);
                tableData.add(entry);**
                }
            System.out.println("*** Database data saved to Observable List named 'data' ***");
            ComboTable.setItems(tableData);
            System.out.println("*** Table Items setted ***");
            statement.close();
        }   catch (SQLException e) {
                System.out.println("Login fehlgeschlagen.");
                e.printStackTrace();
        }
    }

}

型号:

    package model;

import javafx.beans.property.SimpleStringProperty;

public class model {

    private final SimpleStringProperty rCol1;
    private final SimpleStringProperty rCol2;

    public model(String sCol1, String sCol2) {
        this.rCol1 = new SimpleStringProperty(sCol1);
        this.rCol2 = new SimpleStringProperty(sCol2);
    }

    public String getRCol1() {
        return rCol1.get();
    }

    public void setRCol1(String set) {
        rCol1.set(set);
    }
    public String getRCol2() {
        return rCol2.get();
    }

    public void setRCol2(String set) {
        rCol2.set(set);
    }
}

应用程序现在看起来像这样: Picture

希望你能帮助我!

The declaration of the Cell works fine, but the comboBox doesn't appear in the table.

当此单元格处于编辑模式时,将出现 ComboBoxTableCell 的组合框。为此,您需要将表格视图设置为可编辑,然后双击上面提到的单元格。

来自 ComboBoxTableCell javadoc 的引用:

By default, the ComboBoxTableCell is rendered as a Label when not being edited, and as a ComboBox when in editing mode. The ComboBox will, by default, stretch to fill the entire table cell.