TableView从列表框javafx插入数据

时间:2015-06-25 10:39:32

标签: javafx tableview

我有一个listBox,其中填充了我的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);

}

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

2 个答案:

答案 0 :(得分:0)

在你正在调用的事件处理程序中

table.setItems(...);

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

使用

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

代替。

答案 1 :(得分:0)

搜索了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);
    }
}
相关问题