如何从javaFX tableView中的选定行获取数据

时间:2016-05-31 12:35:13

标签: java javafx

我有一个删除某一行的按钮,但它不会从数据库中删除它。我重新加载TableView它重新出现。我该怎么办呢帮助..

我的控制器

 @FXML
private Button deleteButton;

private static Connection conn=null;

private ObservableList<UserData> data;

@FXML
private TableView<UserData> clerksTableView;
@FXML
private TableColumn<UserData,String> ColumnName;
@FXML
private TableColumn<UserData,String> ColumnEmail;
@FXML
private TableColumn<UserData,String> ColumnLocation;
@FXML
private TableColumn<UserData,String> ColumnPassword;
@FXML
private TableColumn<UserData,String> Columnid;



@Override
public void initialize(URL url, ResourceBundle rb) {
    populateTable();
}

private void populateTable() {

    Connection conn = null;
    PreparedStatement preparedStatement;
    ResultSet resultSet;
    try {
        conn = DBconn.DBconnect();

        String sql = "SELECT * FROM clerk";
        preparedStatement = (PreparedStatement) conn.prepareStatement(sql);

        resultSet = preparedStatement.executeQuery();
        data = FXCollections.observableArrayList();
        UserData userDataInstance;

        while (resultSet.next()) {
            userDataInstance = new UserData(resultSet.getString("id"),resultSet.getString("fullname"),resultSet.getString("email"),resultSet.getString("location"),resultSet.getString("pass"));
            int id = Integer.parseInt(resultSet.getString("id"));
            data.add(userDataInstance);

        }

    } catch (SQLException ex) {
        System.out.println("Connection Failed! Check output console" + ex.getMessage());
        JOptionPane.showMessageDialog(null, "Connection Error",  "Error", JOptionPane.WARNING_MESSAGE);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    Columnid.setCellValueFactory(new PropertyValueFactory<UserData,String>("id"));
    ColumnName.setCellValueFactory(new PropertyValueFactory<UserData,String>("fullname"));
    ColumnEmail.setCellValueFactory(new PropertyValueFactory<UserData,String>("email"));
    ColumnLocation.setCellValueFactory(new PropertyValueFactory<UserData,String>("location"));
    ColumnPassword.setCellValueFactory(new PropertyValueFactory<UserData,String>("password"));

    clerksTableView.setItems(data);


}

public void onDelete(ActionEvent actionEvent) {

    ObservableList<UserData> selectedRow,allRows;
    allRows = clerksTableView.getItems();
    selectedRow = clerksTableView.getSelectionModel().getSelectedItems();

    selectedRow.forEach(allRows :: remove);
}

我的.fxml文件

<AnchorPane fx:id="ViewClerks" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="641.0" prefWidth="660.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Controllers.ClerksView">
  <children>
      <SplitPane dividerPositions="0.7204724409448819" layoutX="140.0" layoutY="121.0" orientation="VERTICAL" prefHeight="476.0" prefWidth="498.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="264.0" prefWidth="496.0">
               <children>
                  <TableView fx:id="clerksTableView" layoutX="164.0" layoutY="14.0" prefHeight="291.0" prefWidth="496.0" AnchorPane.bottomAnchor="-32.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                    <columns>
                        <TableColumn fx:id="Columnid" prefWidth="43.0" text="id" />
                      <TableColumn fx:id="ColumnName" prefWidth="160.0" text="Full name" />
                      <TableColumn fx:id="ColumnEmail" prefWidth="157.0" text="Email" />
                        <TableColumn fx:id="ColumnLocation" prefWidth="145.0" text="Location" />
                        <TableColumn fx:id="ColumnPassword" maxWidth="200.0" prefWidth="152.0" text="Password" />
                    </columns>
                  </TableView>
               </children>
            </AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="173.0" prefWidth="638.0">
               <children>
                  <TextField fx:id="ClerksFullname" layoutX="281.0" layoutY="16.0" promptText="Full name" />
                  <TextField fx:id="ClerksEmailAddress" layoutX="281.0" layoutY="51.0" prefHeight="26.0" prefWidth="171.0" promptText="Email" />
                  <TextField fx:id="ClerksLocation" layoutX="281.0" layoutY="84.0" prefHeight="26.0" prefWidth="171.0" promptText="Location" />
                  <TextField fx:id="ClerksPassword" layoutX="281.0" layoutY="126.0" promptText="Password" />
                  <Button layoutX="502.0" layoutY="47.0" mnemonicParsing="false" onAction="#addingClerksDetails" prefHeight="60.0" prefWidth="116.0" stylesheets="@../css/portal.css" text="Add Clerk" />
                  <Separator layoutX="235.0" layoutY="13.0" orientation="VERTICAL" prefHeight="144.0" prefWidth="6.0" />
                  <Label fx:id="successLabel" layoutX="520.0" layoutY="34.0" stylesheets="@../css/portal.css" />
                  <Button fx:id="deleteButton" layoutX="41.0" layoutY="97.0" mnemonicParsing="false" onAction="#onDelete" prefHeight="26.0" prefWidth="130.0" stylesheets="@../css/portal.css" text="Delete Clerk"   textFill="#f50000" />
               </children></AnchorPane>
        </items>
      </SplitPane>
   </children>
</AnchorPane>

我尝试在线搜索如何从所选行中获取每个单元格的数据,以便从我的数据库中删除它但无济于事。 我是javaFX的新手

这里有更多代码

public class UserData {
    private SimpleStringProperty fullname;
    private SimpleStringProperty email;
    private SimpleStringProperty location;
    private SimpleStringProperty password;
    private SimpleStringProperty id;


    public UserData(String id, String fullname, String email, String location, String password) {
        this.fullname = new SimpleStringProperty(fullname);
        this.email = new SimpleStringProperty(email);
        this.location = new SimpleStringProperty(location);
        this.password = new SimpleStringProperty(password);
        this.id = new SimpleStringProperty(id);
    }

    public String getfullname() {
        return fullname.get();
    }

    public void setfullname(String fName) {
        fullname.set(fName);
    }
    public StringProperty fullnameProperty() {
        return fullname;
    }

    public String getemail() {
        return email.get();
    }

    public void setemail(String Email) {
        fullname.set(Email);
    }
    public StringProperty emailProperty() {
        return email;
    }

    public String getlocation() {
        return location.get();
    }

    public void setlocation(String Location) {
        fullname.set(Location);
    }
    public StringProperty locationProperty() {
        return location;
    }

    public String getpassword() {
        return password.get();
    }

    public void setpassword(String Pass) {
        fullname.set(Pass);
    }
    public StringProperty passwordProperty() {
        return password;
    }

    public String getid() {
        return id.get();
    }

    public void setid(String Id) {
        id.set(Id);
    }
    public StringProperty idProperty() {
        return id;
    }
}

1 个答案:

答案 0 :(得分:0)

您必须调用数据库以删除所选行,之后它会自动从表中删除该行。