TableView从列表框javafx插入数据

TableView从列表框javafx插入数据,javafx,tableview,Javafx,Tableview,我有一个列表框,其中填充了mysql数据库中的一些数据。我希望在listview的tableView中添加所选项目。问题是,当我单击“添加”按钮时,所选项目将添加到第一个单元格中。但是,当我选择另一个项目,然后单击“添加”时,它会覆盖在第一个单元格中。 这是我的密码 Button Select= new Button("Select"); private TableView table = new TableView<>(); public void start(Stage prim

我有一个列表框,其中填充了mysql数据库中的一些数据。我希望在listview的tableView中添加所选项目。问题是,当我单击“添加”按钮时,所选项目将添加到第一个单元格中。但是,当我选择另一个项目,然后单击“添加”时,它会覆盖在第一个单元格中。 这是我的密码

Button Select= new Button("Select");
private TableView table = new TableView<>();
public void start(Stage primaryStage) throws Exception{
TableColumn<Map, String> NameCol = new TableColumn<>("NameCol");
TableColumn<Map, String> QuantityCol = new TableColumn<>("Quantity");
TableColumn<Map, String> PriceCol = new TableColumn<>("Price");
ObservableList <String> items = FXCollections.observableArrayList (
        "0" , "1" , "2" , "3" , "4" ,
              "5" , "6" , "7" , "8" , "9" );

    table.getColumns().addAll(NameCol, QuantityCol, PriceCol);

    QuantityCol.setCellFactory(ChoiceBoxTableCell.forTableColumn (items));
    table.setItems(generateDataInMap());
    table.setEditable(true);
    table.getSelectionModel().setCellSelectionEnabled(true);
    table.getColumns().setAll(NameCol, QuantityCol,PriceCol);

Select.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
               try {

                  table.setItems(generateDataInMap());
                  NameCol.setCellValueFactory(new MapValueFactory(Column1MapKey));
       } catch (Exception ex) {
           Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
    });

private ObservableList<Map> generateDataInMap() {

     int count = 1;
     ObservableList<Map> allData = FXCollections.observableArrayList();

        Map<String, String> dataRow = new HashMap<>();
        String value1 = nameView.getSelectionModel().getSelectedItem();

        dataRow.put(Column1MapKey, value1);

        allData.add(dataRow);
        System.out.println(dataRow);
    return allData;
}

public static void main(String[] args) {
    launch(args);

}
按钮选择=新建按钮(“选择”);
private TableView table=new TableView();
public void start(Stage primaryStage)引发异常{
TableColumn NameCol=新的TableColumn(“NameCol”);
TableColumn QuantityCol=新的TableColumn(“数量”);
TableColumn PriceCol=新的TableColumn(“价格”);
ObservableList items=FXCollections.observableArrayList(
"0" , "1" , "2" , "3" , "4" ,
"5" , "6" , "7" , "8" , "9" );
table.getColumns().addAll(NameCol、QuantityCol、PriceCol);
QuantityCol.setCellFactory(ChoiceBoxTableCell.forTableColumn(项目));
table.setItems(generatedatanmap());
table.setEditable(true);
table.getSelectionModel().setCellSelectionEnabled(true);
table.getColumns().setAll(NameCol、QuantityCol、PriceCol);
Select.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
试一试{
table.setItems(generatedatanmap());
NameCol.setCellValueFactory(新MapValueFactory(Column1MapKey));
}捕获(例外情况除外){
Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
私有ObservableList GenerateDainMap(){
整数计数=1;
ObservableList allData=FXCollections.observableArrayList();
Map dataRow=newhashmap();
字符串值1=nameView.getSelectionModel().getSelectedItem();
dataRow.put(Column1MapKey,value1);
allData.add(dataRow);
System.out.println(数据行);
返回所有数据;
}
公共静态void main(字符串[]args){
发射(args);
}

我没有写所有的代码,只有代码块。有人能帮我一下吗?我会非常感激:):)

在您调用的事件处理程序中

table.setItems(...);
设置表中显示的项目(即用指定的项目替换表中的所有项目)

使用


相反。

在搜索了4天之后,我编写了此代码……它可以工作。) 如果有人需要它,它写在下面

public class mainMenu extends Application {
private TableView<foodTable> table = new TableView<foodTable>();
private final ObservableList<foodTable> data = FXCollections.observableArrayList();   

Select.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
               try {
                 String FoodName = new String(nameView.getSelectionModel().getSelectedItem());             
                 data.add(new foodTable(String.valueOf(rowid),FoodName,qtycb.getValue().toString(),String.valueOf(PriceAmtVal)));
                 rowid++;
                 System.out.println(data);
       } catch (Exception ex) {
           Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
    });
//Buttion code :

Select.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
               try {
                 String FoodName = new String(nameView.getSelectionModel().getSelectedItem());             
                 data.add(new foodTable(String.valueOf(rowid),FoodName,qtycb.getValue().toString(),String.valueOf(PriceAmtVal)));
                 rowid++;
                 System.out.println(data);
       } catch (Exception ex) {
           Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
    });
public static class foodTable {

    private final SimpleStringProperty FoodName;
    private final SimpleStringProperty Quantity;
    private final SimpleStringProperty Price;

    private foodTable(String fName, String lName, String email,String Price) {

        this.FoodName = new SimpleStringProperty(lName);
        this.Quantity = new SimpleStringProperty(email);
        this.Price = new SimpleStringProperty(Price);
    }


    public String getFoodName() {
        return FoodName.get();
    }
    public void setFoodName(String fName) {
        FoodName.set(fName);
    }

    public String getQuantity() {
        return Quantity.get();
    }
    public void setQuantity(String fName) {
        Quantity.set(fName);
    }
    public String getPrice() {
        return Price.get();
    }
    public void setPrice(String fName) {
        Price.set(fName);
    }
}
public类主菜单扩展应用程序{
private TableView table=new TableView();
私有最终ObservableList数据=FXCollections.observableArrayList();
Select.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
试一试{
String FoodName=新字符串(nameView.getSelectionModel().getSelectedItem());
添加(新的foodTable(String.valueOf(rowid)、FoodName、qtycb.getValue().toString()、String.valueOf(PriceAmtVal));
rowid++;
系统输出打印项次(数据);
}捕获(例外情况除外){
Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
//对接代码:
Select.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent e){
试一试{
String FoodName=新字符串(nameView.getSelectionModel().getSelectedItem());
添加(新的foodTable(String.valueOf(rowid)、FoodName、qtycb.getValue().toString()、String.valueOf(PriceAmtVal));
rowid++;
系统输出打印项次(数据);
}捕获(例外情况除外){
Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
公共静态类食物表{
私有最终SimpleStringProperty食品名称;
私有财产数量;
私人物业价格;
私有foodTable(字符串fName、字符串lName、字符串电子邮件、字符串价格){
this.FoodName=新的SimpleStringProperty(lName);
this.Quantity=新的SimpleStringProperty(电子邮件);
此.Price=新SimpleStringProperty(价格);
}
公共字符串getFoodName(){
返回FoodName.get();
}
公共void setFoodName(字符串fName){
FoodName.set(fName);
}
公共字符串getQuantity(){
返回数量。get();
}
公共void setQuantity(字符串fName){
数量集(fName);
}
公共字符串getPrice(){
返回价格。get();
}
public void setPrice(字符串fName){
Price.set(fName);
}
}

是的,我试过了..使用它也会替换第一个单元格中的数据。我需要添加第二个单元格,即它下面的单元格..我对javafx非常陌生。如果你能给我一个例子,我将替换
表.setItems(generatedatanmap());
表.getItems().addAll(generatedatanmap())
第1个单元格的书写仍在进行中:(你能帮我一下吗:(
public class mainMenu extends Application {
private TableView<foodTable> table = new TableView<foodTable>();
private final ObservableList<foodTable> data = FXCollections.observableArrayList();   

Select.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
               try {
                 String FoodName = new String(nameView.getSelectionModel().getSelectedItem());             
                 data.add(new foodTable(String.valueOf(rowid),FoodName,qtycb.getValue().toString(),String.valueOf(PriceAmtVal)));
                 rowid++;
                 System.out.println(data);
       } catch (Exception ex) {
           Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
    });
//Buttion code :

Select.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
               try {
                 String FoodName = new String(nameView.getSelectionModel().getSelectedItem());             
                 data.add(new foodTable(String.valueOf(rowid),FoodName,qtycb.getValue().toString(),String.valueOf(PriceAmtVal)));
                 rowid++;
                 System.out.println(data);
       } catch (Exception ex) {
           Logger.getLogger(mainMenu.class.getName()).log(Level.SEVERE, null, ex);
       }
   }
    });
public static class foodTable {

    private final SimpleStringProperty FoodName;
    private final SimpleStringProperty Quantity;
    private final SimpleStringProperty Price;

    private foodTable(String fName, String lName, String email,String Price) {

        this.FoodName = new SimpleStringProperty(lName);
        this.Quantity = new SimpleStringProperty(email);
        this.Price = new SimpleStringProperty(Price);
    }


    public String getFoodName() {
        return FoodName.get();
    }
    public void setFoodName(String fName) {
        FoodName.set(fName);
    }

    public String getQuantity() {
        return Quantity.get();
    }
    public void setQuantity(String fName) {
        Quantity.set(fName);
    }
    public String getPrice() {
        return Price.get();
    }
    public void setPrice(String fName) {
        Price.set(fName);
    }
}