Javafx 如何在从comboboxtablecell中选择相应项时同时更改tableview中的值

Javafx 如何在从comboboxtablecell中选择相应项时同时更改tableview中的值,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我试过了,但遇到了一个问题,我从comboboxtablecell获取所选项目的相应值,但无法将这些值添加到表视图中的相应列中 Controller.java public class controller { GetConnection gc = new GetConnection(); PreparedStatement pst; ResultSet rs;

我试过了,但遇到了一个问题,我从comboboxtablecell获取所选项目的相应值,但无法将这些值添加到表视图中的相应列中

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

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

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

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

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

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

        }
        }
上面的代码是可运行和简化的,程序有一个tableview和三列,其中第一列单元格是一个ComboxTableCell,其中有项目。第二列单元格是一个可编辑的文本字段,第三列单元格是一个ComboxTableCell,其中有项目在数据库中。我自己也尝试过从哪里获取相应的值每当我在category.java System.out.println(“categoryis”+category1)的组合框表格单元格中选择值时,行中的值; System.out.println(“单价为”+price1)。每当我在表视图的组合框中选择项时,请帮助我更改表视图中的值。

您需要 转到您的手机出厂代码 1为组合框选择更改添加事件处理程序 获取当前行 获取当前项目
更改所需的值

示例组合框所选项目更改tableview示例的值

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

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


请使用IDE的代码格式快捷方式。我保证你会喜欢的!所以有3列,其中2列有组合框。为什么不删除不相关的列,只留下一个与您的问题相关的列呢。让我们首先明确并简化代码。WDYT?嗯,代码可能是可运行的-在您的环境中,而不是在我们的环境中;-)您必须用模拟替换数据库访问,像f.i.一个查找项目的映射/列表被引导关闭,因为不清楚您的要求-请将其归结为一个问题,并使其成为真正的SSCCE1。我上面的代码具有处理选择更改的事件处理程序,并且正在从行中获取特定值。2我的问题是我获取的值是一个字符串值字符串category1=rs.getString(1);这是我的代码的一部分。3.请帮助我找到如何将字符串值放入setcellvaluefactory的解决方案。非常抱歉,您的描述很难理解-作为程序员,我们有一种通用语言,即代码:-)。为了更好地理解,请编写该处理程序,而不是描述它。