如何使用Javafx中的按钮在tableview中添加新行

时间:2015-12-04 02:28:07

标签: java tableview javafx-8

如何使用按钮添加新行?这是我创建的代码片段。 它只添加一行,每次单击按钮时,它都会用新的一行替换第一行。我希望它添加一个新行而不是替换它。

public void handleButtonAction(javafx.event.ActionEvent event) {
    String search = searchtxt.getText();

    String squery = "SELECT * FROM SERVER where HOSTNAME = '" + search + "'";

    host_col.setCellValueFactory(new PropertyValueFactory<>("Host"));
    site_col.setCellValueFactory(new PropertyValueFactory<>("Site"));
    loc_col.setCellValueFactory(new PropertyValueFactory<>("Loc"));
    rack_col.setCellValueFactory(new PropertyValueFactory<>("Rack"));
    row_col.setCellValueFactory(new PropertyValueFactory<>("Row"));
    make_col.setCellValueFactory(new PropertyValueFactory<>("Make"));
    model_col.setCellValueFactory(new PropertyValueFactory<>("Model"));
    serial_col.setCellValueFactory(new PropertyValueFactory<>("Serial"));
    try {
        ObservableList<Object> data = FXCollections.observableArrayList();
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:SYSTEM/renz@localhost:1521:XE");
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery(squery);
        if (rs.next()) {
            String host = rs.getString(1);
            String site = rs.getString(2);
            String loc = rs.getString(3);
            String rack = rs.getString(4);
            String roww = rs.getString(5);
            String make = rs.getString(6);
            String model = rs.getString(7);
            String serial = rs.getString(8);
            TableEntry record = new TableEntry(host, site, loc, rack, roww, make, model, serial);
            data.add(record);
            table.setItems(data);
        }

    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

这是TableEntry类:

public class TableEntry {

private String host_entry;
private String site_entry;
private String loc_entry;
private String rack_entry;
private String row_entry;
private String make_entry;
private String model_entry;
private String serial_entry;

public TableEntry(String a, String b, String c, String d, String e, String f, String g, String h) {
    this.host_entry = a;
    this.site_entry = b;
    this.loc_entry = c;
    this.rack_entry = d;
    this.row_entry = e;
    this.make_entry = f;
    this.model_entry = g;
    this.serial_entry = h;
}

public String getHost() {
    return host_entry;
}

public void setHost(String a) {
    host_entry = a;
}

public String getSite() {
    return site_entry;
}

public void setSite(String b) {
    site_entry = b;
}

public String getLoc() {
    return loc_entry;
}

public void setLoc(String c) {
    loc_entry = c;
}

public String getRack() {
    return rack_entry;
}

public void setRack(String d) {
    rack_entry = d;
}

public String getRow() {
    return row_entry;
}

public void setRow(String e) {
    row_entry = e;
}

public String getMake() {
    return make_entry;
}

public void setMake(String f) {
    make_entry = f;
}

public String getModel() {
    return model_entry;
}

public void setModel(String g) {
    model_entry = g;
}

public String getSerial() {
    return serial_entry;
}

public void setSerial(String h) {
    serial_entry = h;
}

}

1 个答案:

答案 0 :(得分:-1)

假设table是&#34; TableView的变量。 将代码修改为

table.getItems().clear;
table.getItems.addAll(record);