2015-05-26 95 views
0

我也跟着从这个问题的说明我的应用程序的TableView中设置ComboBoxCell。 (How to put ComboBoxTableCell in a TableView?集ComboBoxCell在TableView中的JavaFX

细胞的申报工作正常,但下拉框中不会出现在表中。我觉得是这样的,因为只有col1和COL2是在我的模型。 COL3不是写在数据库连接后,我的表项

我不知道如何takte 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/[email protected]: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

希望你能帮助我!

+0

当您双击单元格中的ComboBoxCell会出现,即进入编辑模式。但首先你需要看到整个图片,你想从combobox分配选定的值。细胞最初会呈现什么等等。 –

+0

谢谢!发布它作为答案,我回答这个问题。 :) – lukas22497

+0

等..所以你的问题解决了? –

回答

0

单元的声明工作正常,但组合框不出现 出现在表中。

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

从ComboBoxTableCell javadoc的名言:

默认情况下,ComboBoxTableCell呈现为一个标签时被编辑不 ,作为一个组合框时编辑模式。默认情况下,组合框 将伸展以填充整个表格单元格。