Впервые в javafx на этапе обучения. Я пытаюсь обновить данные из базы данных, но при вызове метода getConnection список обновляется, но Treeitem не обновляется в представлении.
На первом на экране отображаются доступные базы данных, но после создания нового представления древовидная таблица не обновляется даже после вызова getConnection ();
помечает место, где необходимо решение, с помощью "// --------- -------- Вызов здесь для обновления"
Tried
1.Используется Refre sh ();
2. попытался очистить вид;
PaneClass. java
public class PanesClass extends Application {
ObservableList<Connections> cList = FXCollections.observableArrayList();
DbConnection dbConnection = new DbConnection();
TreeItem rootItem = new TreeItem();
TreeTableView activeConnections = new TreeTableView();
AnchorPane firstPane = new AnchorPane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
NewConnection newConnection = new NewConnection();
SplitPane root = new SplitPane();
AnchorPane secondPane = new AnchorPane();
HBox buttonBox = new HBox();
BorderPane topBar = new BorderPane();
Button nConnection = new Button("+");
buttonBox.getChildren().addAll(nConnection);
topBar.setTop(buttonBox);
TreeTableColumn<String, Connections> cNameColoumn = new TreeTableColumn<>("Name");
cNameColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("name"));
TreeTableColumn<String, Connections> cStatusColoumn = new TreeTableColumn<>("Status");
cStatusColoumn.setCellValueFactory(new TreeItemPropertyValueFactory<>("status"));
activeConnections.getColumns().addAll(cNameColoumn, cStatusColoumn);
activeConnections.setLayoutX(20);
activeConnections.setLayoutY(40);
firstPane.getChildren().addAll(topBar, activeConnections);
root.getItems().addAll(firstPane, secondPane);
Scene sc = new Scene(root, 600, 480);
primaryStage.setScene(sc);
primaryStage.show();
activeConnections.setShowRoot(false);
getconnection();
nConnection.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
newConnection.getConnection(activeConnections);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
});
}
void getconnection() throws ClassNotFoundException, SQLException {
rootItem.getChildren().clear();
cList.clear();
cList = dbConnection.getDatabase();
for (Connections temp : cList) {
rootItem.getChildren().add(new TreeItem<Connections>(temp));
System.out.println(temp);
}
activeConnections.setRoot(rootItem);
activeConnections.refresh();
}
}
NewConnection. java
public class NewConnection {
Connections connection = null;
ObservableList<Connections> cList = FXCollections.observableArrayList();
PanesClass panesClass = new PanesClass();
TreeItem<Connections> cItem = null;
TreeItem rootItem = new TreeItem();
public void getConnection(TreeTableView<Connections> activeConnections) throws ClassNotFoundException, SQLException {
DbConnection dbConnection=new DbConnection();
Stage secondaryStage = new Stage();
VBox root = new VBox();
GridPane cDetails = new GridPane();
HBox actionButtons = new HBox();
Button connect = new Button("Connect");
Button save = new Button("Save");
Button cancel = new Button("Cancel");
actionButtons.getChildren().addAll(connect, save, cancel);
//cList=dbConnection.getDatabase();
actionButtons.setSpacing(10);
Label name = new Label("Username : ");
cDetails.add(name, 0, 0);
TextField uName = new TextField();
cDetails.setHgrow(uName, Priority.ALWAYS);
cDetails.add(uName, 1, 0);
cDetails.setVgap(10);
root.getChildren().addAll(cDetails, actionButtons);
Scene sc = new Scene(root, 500, 200);
secondaryStage.setScene(sc);
secondaryStage.initModality(Modality.APPLICATION_MODAL);
secondaryStage.show();
save.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
try {
dbConnection.setNewDatabase(uName.getText());
panesClass.getconnection();//-----------------*Calling Here for Updation*
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
secondaryStage.close();
event.consume();
}
});
}
}
Подключения. java
public class Connections {
private String name;
private String password;
private String url;
public Connections() {
super();
}
public Connections(String name, String password, String url) {
super();
this.name = name;
this.password = password;
this.url = url;
}
public Connections(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "Connections [name=" + name + ", password=" + password + ", url=" + url + "]";
}
}