New to javafx in learning stage.I am trying to Update the data from the database but when the call the getConnection method the list is updated but the Treeitem is not updated in the view.
On the first screen it shows the available Db's but after creating new the tree table view is not updating even after calling the getConnection();
marked the place where needed solution with "//-----------------Calling Here for Updation"
Tried
1.Used Refresh();
2.tried to clear the view;
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();
}
});
}
}
Connections.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 + "]";
}
}