TableView从列表框javafx插入数据



我有一个listBox,其中填充了来自mysql数据库的一些数据。我希望在表中添加所选项目从列表视图查看。问题是,当我单击"添加"按钮时,所选项目会添加到第一个单元格中。但是,当我选择另一个项目,然后单击"添加"时,它会在第一个单元格中被覆盖。这是我的密码。

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

我没有写所有的代码,只是代码块。有人能帮帮我吗。我真的很感激:):)

在事件处理程序中,您正在调用

table.setItems(...);

它设置了表中显示的项目(即用您指定的项目替换所有项目)。

使用

table.getItems().addAll(...);

相反。

在搜索了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);
    }
}

最新更新