How to change values in tableview simultaneously w

2019-09-11 07:25发布

I tried but I got stuck at a point where am getting the corresponding values of the selected item from comboboxtablecell but cannot add the values to the corresponding column in the table view

Controller.java

  public class controller   {
        GetConnection gc = new GetConnection();
                PreparedStatement pst;
                ResultSet rs;
                        Statement st;
                  private ObservableList<Users> datas = FXCollections.observableArrayList();
                 public controller(){}
        @FXML
        private TableView<Users> table;
        @FXML
        private TableColumn<Users,String> c1;
       @FXML
        private TableColumn<Users,String> c2;
        @FXML
        private TableColumn<Users,String> c3;
       @FXML
       private void editable() {

        try {
             ObservableList<Users> datas = FXCollections.observableArrayList();
            ObservableList<String> item = FXCollections.observableArrayList();
            ObservableList<String> iprice = FXCollections.observableArrayList();
            String sql = "select * from itemsadd";
            pst = gc.getConnection().prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                String name = rs.getString("itemcode");
                String cat=rs.getString("unitprice");
                item.add(name);
                iprice.add(cat);
                System.out.println("probs" + item);
            }
            ResultSet rs2 = gc.getConnection().createStatement()
                    .executeQuery("SELECT * FROM itemsadd WHERE itemcode=1001");
             while (rs2.next()) {
                    datas.add(new Users(rs2.getString("itemcode"),rs2.getString("category"),rs2.getString("unitprice")));
             }
                    c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
            c1.setCellFactory(ComboBoxTableCell.forTableColumn(item));

                for(String name:item){
                     c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
                c1.setCellFactory(ComboBoxTableCell.forTableColumn(item));
                          System.out.println("hell3"+name);
       }c1.setOnEditCommit( ( TableColumn.CellEditEvent<Users, String> e ) ->
            {
                String newValue = e.getNewValue();
                    int index = e.getTablePosition().getRow();
                    System.out.println("position"+index);
               try{ 
        System.out.println("new values"+newValue);
        String dsql="SELECT category,unitprice FROM itemsadd WHERE itemcode=?;";
        pst=gc.getConnection().prepareStatement(dsql);
        pst.setString(1, newValue); //this replaces the 1st  "?" in the query for username
        rs=pst.executeQuery();
        while(rs.next())
                        {
                            String category1 = rs.getString(1);
                            String price1 = rs.getString(2);


                        System.out.println("category is"+category1);
                        System.out.println("unitprice is"+price1);


                        }
            }catch(Exception ed){} } );
                c2.setCellValueFactory(new PropertyValueFactory("category"));
                c2.setCellFactory(TextFieldTableCell.forTableColumn());
                c3.setCellValueFactory(new PropertyValueFactory("unitprice"));
                c3.setCellFactory(ComboBoxTableCell.forTableColumn(iprice));
                table.setEditable(true);
                table.getItems().clear();
                table.setItems(datas);

              } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Error on Building Data");
            }
        }

    public static class Users {
      private StringProperty Itemc;
      private StringProperty category;
      private StringProperty unitprice;
           private Users(String Itemc,String category,String unitprice) {
           this.Itemc= new SimpleStringProperty(Itemc);
           this.category=new SimpleStringProperty(category);
           this.unitprice=new SimpleStringProperty(unitprice);
    }
      public String getItemc() {
            return Itemc.get();
        }
       public void setItemc(String Itemc) {
            this.Itemc.set(Itemc);
        }
      public StringProperty ItemcProperty() {
                return Itemc;
            }
      public String getcategory() {
            return category.get();
        }
       public void setcategory(String category) {
            this.category.set(category);
        }
      public StringProperty categoryProperty() {
                return category;
            }
     public String getunitprice() {
            return unitprice.get();
        }
       public void setunitprice(String unitprice) {
            this.unitprice.set(unitprice);
        }
      public StringProperty unitpriceProperty() {
                return unitprice;
            }}

    }

Table.fxml

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication47.controller">
       <children>
          <TableView fx:id="table" editable="true" layoutX="136.0" layoutY="58.0" onKeyPressed="#editable" prefHeight="200.0" prefWidth="335.0">
            <columns>
              <TableColumn fx:id="c1" prefWidth="116.0" text="Code" />
                <TableColumn fx:id="c2" prefWidth="115.0" text="Address" />
                <TableColumn fx:id="c3" prefWidth="102.0" text="Price" />
            </columns>
          </TableView>
       </children>
    </AnchorPane>

Tableview.java

    public class Tableveiw extends Application {
             private Stage primaryStage;
            private AnchorPane pane;
            @Override
            public void start(Stage primaryStage) {
                this.primaryStage = primaryStage;
                this.primaryStage.setTitle("AddressApp");
                showPerson();
            }
              public void showPerson() {
                try {
                    // Load root layout from fxml file.
                    FXMLLoader loader = new FXMLLoader();
                    loader.setLocation(Tableveiw.class
                            .getResource("table.fxml"));
                    pane= (AnchorPane) loader.load();

                    // Show the scene containing the root layout.
                    Scene scene = new Scene(pane);
                    primaryStage.setScene(scene);


                    primaryStage.show();
                } catch (IOException e) {
                    e.printStackTrace();
                }}
            public static void main(String[] args) {
                launch(args);
            }

        }

GetConnection.java

public class GetConnection{
     public Connection getConnection() throws Exception
        {
        Connection con=null;
            try {
                        System.out.println("MySQL Connect Example.");
                        String url = "jdbc:mysql://localhost:3306/";
                        String dbName = "login";
                        String driver = "com.mysql.jdbc.Driver";
                        Class.forName(driver).newInstance();
                        con = DriverManager.getConnection(url+dbName,"root","");
      } catch (Exception e) {
                e.printStackTrace();
      }
              return con;

      }
    public static void main(String arg[])
        {
            GetConnection con =new GetConnection();
            System.out.println("Connection"+con);

        }
        }

The above code is runnable and simplified and the program has a tableview and three column with first columncell is a comboboxtablecell having items in it.The second columncell is a editable text field and third column cell is a comboboxtablecell with its items in database.I have tried myself where am getting values of the corresponding values in the row whenever I select value in combo box table cell in category.java System.out.println("categoryis"+category1); System.out.println("unitprice is"+price1); .Please help me to change values in tableview whenever I select item in the combobox in table view.

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-11 07:37

You need to Go to your cell factory code 1add event handler for combobox selection change Get current row Get current item Change the value you need

查看更多
爷的心禁止访问
3楼-- · 2019-09-11 07:39

Sample ComboBox Selected items changing values of tableview example.

Tablecombo.java

public class Tablecombo extends Application {
    Stage primaryStage;
    Scene scene;
    String username;
    AnchorPane anchorpane;
    BorderPane borderpane;
   // Pane pane;
    BorderPane border;
    Stage sstage;
     @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
       this.primaryStage.setTitle("Login");
root();
    }
         public void root() {
        try {          
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(Tablecombo.class.getResource("tcombo.fxml"));
             anchorpane = (AnchorPane) loader.load();
             Stage dialogStage = new Stage();
            dialogStage.setTitle("Main");
            dialogStage.initModality(Modality.WINDOW_MODAL);
            Scene scene = new Scene(anchorpane);
            dialogStage.setScene(scene);
dialogStage.showAndWait();
        } catch (IOException e) {
            e.printStackTrace();
                 }
   }
    public static void main(String[] args) {
        launch(args);
    }
    }

tablecontroller.java

public class tablecontroller   {
        Tablecombo main;
        GetConnection gc = new GetConnection();
                PreparedStatement pst;
                ResultSet rs;


                  public tablecontroller(){


                  }
              @FXML
            private TableView<UserData> table4;

          @FXML
        private TableColumn<UserData,String> c1;
            @FXML
        private TableColumn<UserData,String> c2;


       @FXML
       private void editable() {

        try {
             ObservableList<UserData> datas = FXCollections.observableArrayList();
            ObservableList<String> names = FXCollections.observableArrayList();
             ObservableList<String> rat = FXCollections.observableArrayList();
           // ObservableList<Users> datas = FXCollections.observableArrayList();
            String sql = "select * from pdfs";
           // String msql="select * from category";
            pst = gc.getConnection().prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {

                String name = rs.getString("name");
                String cat=rs.getString("country");
                rat.add(cat);
                names.add(name);
                System.out.println("probs" + names);
            }
            ResultSet rs2 = gc.getConnection().createStatement().executeQuery("SELECT * FROM pdfs LIMIT 1");
    //ObservableList<Users> datas = FXCollections.observableArrayList();
             while (rs2.next()) {
                    datas.add(new UserData(rs2.getString("name"),rs2.getString("country")));
             }
                    c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
            c1.setCellFactory(ComboBoxTableCell.forTableColumn(names));

                for(String name:names){
                     c1.setCellValueFactory(new PropertyValueFactory("Itemc"));
               // System.out.println("hell2"+name);
                c1.setCellFactory(ComboBoxTableCell.forTableColumn(names));
                          System.out.println("hell3"+name);
       }
                 c1.setOnEditCommit( ( TableColumn.CellEditEvent<UserData, String> e ) ->
            {
                String newValue = e.getNewValue();
                    int index = e.getTablePosition().getRow();
                    System.out.println("position"+index);

               try{ 
                   ObservableList<UserData> data = FXCollections.observableArrayList();
    System.out.println("new values"+newValue);
    String dsql="SELECT * FROM pdfs WHERE name=?;";

       pst=gc.getConnection().prepareStatement(dsql);
       System.out.println("quer"+dsql);
       pst.setString(1, newValue); //this replaces the 1st  "?" in the query for username
       rs=pst.executeQuery();
       while(rs.next())
                        {
                             data.add(new UserData(rs.getString("name"),rs.getString("country")));
                        } 

          table4.setItems(data);
       String a = rs.getString(1);
                            String b = rs.getString(2);
                              String c = rs.getString(3);

                        System.out.println("su"+a);
                        System.out.println("ma"+b);
                        System.out.println("man"+c);


               }catch(Exception ed){} } );

                c2.setCellValueFactory(new PropertyValueFactory("quantity"));
                c2.setCellFactory(TextFieldTableCell.forTableColumn());

               table4.setEditable(true);

            table4.setItems(datas);

              } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Error on Building Data");
            }

        }
      } 

UserData.java

   public class UserData {
   private StringProperty Itemc;
    private StringProperty quantity;
            public UserData(String Itemc,String quantity) {
                //this.Quantity = new SimpleStringProperty(Quantity);

                  this.Itemc= new SimpleStringProperty(Itemc);
                   this.quantity = new SimpleStringProperty(quantity);

            }


          public String getItemc() {
                return Itemc.get();
            }

           public void setItemc(String Itemc) {
                this.Itemc.set(Itemc);
            }
          public StringProperty ItemcProperty() {
                    return Itemc;
                }
           public void setquantity(String quantity) {
                this.quantity.set(quantity);
            }
          public String getquantity() {
                return quantity.get();
            }
          public StringProperty quantityProperty() {
                    return quantity;
                }

            }

tcombo.fxml

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tablecombo.tablecontroller">
       <children>
          <TableView fx:id="table4" layoutX="168.0" layoutY="81.0" onKeyPressed="#editable" prefHeight="200.0" prefWidth="230.0">
            <columns>
              <TableColumn fx:id="c1" prefWidth="75.0" text="name" />
              <TableColumn fx:id="c2" prefWidth="75.0" text="country" />
            </columns>
          </TableView>
       </children>
    </AnchorPane>
查看更多
登录 后发表回答