2017-11-25 334 views
0

我想通過選擇組合框項目從數據庫顯示數據。 Combobox包含我的數據庫中用戶可以選擇的列。我嘗試了這一點,combobox是好的,代碼工作沒有錯誤,但表不更新。JavaFX:通過選定的組合框更新tableview上下文(綁定)

控制器類:

package application; 

    import java.io.IOException; 
    import java.net.URL; 
    import java.sql.PreparedStatement; 
    import java.sql.ResultSet; 
    import java.util.ResourceBundle; 

    import javafx.collections.FXCollections; 
    import javafx.collections.ObservableList; 
    import javafx.event.ActionEvent; 
    import javafx.fxml.FXML; 
    import javafx.fxml.FXMLLoader; 
    import javafx.fxml.Initializable; 
    import javafx.scene.Node; 
    import javafx.scene.Parent; 
    import javafx.scene.Scene; 
    import javafx.scene.control.ComboBox; 
    import javafx.scene.control.TableColumn; 
    import javafx.scene.control.TableView; 
    import javafx.scene.control.cell.PropertyValueFactory; 
    import javafx.stage.Stage; 

     public class SortareDupaAngajatiController implements Initializable{ 
      final ObservableList<tableAngajati> data=FXCollections.observableArrayList(); 
      final ObservableList<String> optiuni=FXCollections.observableArrayList(); 
      PreparedStatement intabel=null; 
      ResultSet rezultate=null; 
      String prequery; 
      String postquery; 

      @FXML 
      public ComboBox<String> meniu; 
      @FXML 
      TableView<tableAngajati> tabelangajati; 

      @FXML 
      private TableColumn<tableAngajati,String> numesediucol; 

      @FXML 
      private TableColumn<tableAngajati,Integer> angajaticol; 

      @Override 
      public void initialize(URL location, ResourceBundle resources) { 

       comboMeniu(); 
       insertInTabel(); 


      } 


      public void comboMeniu(){ 
       try { 
        String query = "SELECT * FROM maginfo"; 
        PreparedStatement dindb = Conexiune.conexiuneBd.prepareStatement(query); 
        ResultSet rezultate = dindb.executeQuery(); 


        while (rezultate.next()) { 
         optiuni.add(rezultate.getString("nume")); 
         meniu.setItems(optiuni); 
        } 
        dindb.close(); 
        rezultate.close(); 
      } 
      catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
      } 

      public void insertInTabel(){ 

     try { 
        prequery="SELECT * FROM sediu where nume='"+meniu.getValue()+"'ORDER BY numarangajati DESC"; 
        postquery=prequery; 
        intabel= Conexiune.conexiuneBd.prepareStatement(postquery); 
        rezultate=intabel.executeQuery(); 

        while(rezultate.next()){ 
         data.add(new tableAngajati(
           rezultate.getString(2), 
           rezultate.getInt(7) 
           )); 


        } 

        intabel.close(); 
        rezultate.close(); 

       } catch (Exception e) { 
        System.err.println(e); 
       } 

       numesediucol.setCellValueFactory(new PropertyValueFactory<>("numesediu")); 
       angajaticol.setCellValueFactory(new PropertyValueFactory<>("numarangajati")); 
       tabelangajati.setItems(null); 
       tabelangajati.setItems(data); 

      }} 

表類:

 

    package application; 

    public class tableAngajati { 
     private String numesediu; 
     private int angajati; 


     public tableAngajati(String numesediu, int angajati) { 
      super(); 
      this.numesediu=numesediu; 
      this.angajati=angajati; 
     } 

     public String getNumesediu(){ 
      return numesediu; 
     } 

     public void setNumesediu(String numesediu){ 
      this.numesediu=numesediu; 
     } 

     public int getNumarangajati(){ 
      return angajati; 
     } 

     public void setNumarangajati(int angajati){ 
      this.angajati=angajati; 
     } 

    } 

非常感謝!

+1

顯示'fxml'文件 –

+0

@mrmcwolf我下面張貼的FXML。謝謝! – AnoGio

回答

0

FXML文件:

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.ComboBox?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.ScrollPane?> 
<?import javafx.scene.control.Separator?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.Pane?> 
<?import javafx.scene.text.Font?> 

<AnchorPane prefHeight="389.0" prefWidth="620.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SortareDupaAngajatiController"> 
    <children> 
     <Separator layoutX="178.0" orientation="VERTICAL" prefHeight="382.0" prefWidth="0.0" /> 
     <Pane layoutX="-9.0" layoutY="-1.0" opacity="0.6" prefHeight="390.0" prefWidth="190.0" style="-fx-background-color: #0074D9;"> 
     <children> 
      <Button layoutX="35.0" layoutY="20.0" mnemonicParsing="false" onAction="#arataHome" prefHeight="100.0" prefWidth="130.0" style="-fx-background-color: #FFF;" text="Home" /> 
      <Button layoutX="35.0" layoutY="146.0" mnemonicParsing="false" onAction="#arataAdaugareSediu" prefHeight="100.0" prefWidth="130.0" style="-fx-background-color: #FFF;" text="Adauga sediu" /> 
      <Button layoutX="35.0" layoutY="272.0" mnemonicParsing="false" onAction="#arataAdaugareMagazin" prefHeight="100.0" prefWidth="130.0" style="-fx-background-color: #FFF;" text="Adauga supermarket" /> 
     </children> 
     </Pane> 
     <Label layoutX="195.0" layoutY="10.0" text="Sortare supermarket-uri dupa nr. de angajati din toate sediile" wrapText="true"> 
     <font> 
      <Font name="Berlin Sans FB" size="16.0" /> 
     </font> 
     </Label> 
     <ScrollPane layoutX="194.0" layoutY="38.0" prefHeight="304.0" prefWidth="414.0"> 
     <content> 
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="361.0" prefWidth="399.0"> 
       <children> 
        <TableView fx:id="tabelangajati" layoutX="1.0" layoutY="30.0" prefHeight="338.0" prefWidth="411.0"> 
        <columns> 
         <TableColumn fx:id="numesediucol" prefWidth="204.0" sortable="false" text="Sedii" /> 
         <TableColumn fx:id="angajaticol" prefWidth="193.0" sortable="false" text="Numar angajati" /> 
        </columns> 
        </TableView> 
        <ComboBox fx:id="meniu" layoutX="100.0" layoutY="3.0" prefHeight="25.0" prefWidth="209.0" promptText="Selectati supermarket-ul" /> 
       </children></AnchorPane> 
     </content> 
     </ScrollPane> 
     <Button layoutX="556.0" layoutY="354.0" mnemonicParsing="false" onAction="#Inapoi" text="Inapoi" /> 
    </children> 
</AnchorPane> 

+0

將'onAction =「#insertInTabel」'添加到'ComboBox fx:id =「meniu」'中。 –

+0

@mrmcwolf非常感謝你!代碼有效,但是當我從菜單中選擇其他項目時,表格不刷新,它包含之前所有選定的項目。 – AnoGio

+0

@mrmcwolf我做了!我添加'data.removeAll(data);'到insertInTabel嘗試。感謝您的關注! – AnoGio