簡體   English   中英

如何從javaFX tableView中的選定行獲取數據

[英]How to get data from the selected row in javaFX tableView

我有一個刪除特定行的按鈕,但它不會將其從數據庫中刪除。 盎司我重新加載它再次出現的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;
    }
}

您必須調用數據庫以刪除選定的行,之后它將自動從表中刪除該行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM