user4776743
user4776743

Reputation:

JavaFX: update tableview context by selected combobox(binding)

I want to display data in tableview from database by selecting combobox item. Combobox contains columns from my database which the user can select. I try this, combobox is ok and code works without errors but table is not updated.

Controller Class:

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);

            }}

table Class:


    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;
        }

    }

Thanks a lot!

Upvotes: 0

Views: 741

Answers (1)

user4776743
user4776743

Reputation:

FXML File:

<?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>

Upvotes: 0

Related Questions