Java 从DB获取数据时,新项在TreeTableView中未从更新?
javafx还处于学习阶段,我正在尝试更新数据库中的数据,但是当调用 getConnection方法列表已更新,但视图中的Treeitem未更新 在第一个屏幕上,它显示可用的数据库,但在创建新数据库后,即使在调用getConnection()之后,树表视图也不会更新 在需要解决方案的地方标记“/--------------调用此处进行更新” 尝试 1.使用刷新() 2.试图澄清观点 PaneClass.javaJava 从DB获取数据时,新项在TreeTableView中未从更新?,java,javafx,Java,Javafx,javafx还处于学习阶段,我正在尝试更新数据库中的数据,但是当调用 getConnection方法列表已更新,但视图中的Treeitem未更新 在第一个屏幕上,它显示可用的数据库,但在创建新数据库后,即使在调用getConnection()之后,树表视图也不会更新 在需要解决方案的地方标记“/--------------调用此处进行更新” 尝试 1.使用刷新() 2.试图澄清观点 PaneClass.java public class PanesClass extends Applicatio
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();
}
}
不管你做什么-不要使用刷新(如果没有其他帮助的话,这是紧急api)。相反,设置您的数据,使视图的自动通知起作用-懒得费力阅读这一大堆代码,请将其剥离到一个只有最少量代码仍然存在问题的位置(不需要db连接,只需模拟它,不需要花哨的视觉效果/布局,除了更新的触发器之外,不需要任何交互……尽可能简单!:)谢谢@kleopatra检查了你的个人资料,这样我就可以联系你了!很好,你一直在尝试:)但这还远远不够(f.i.当您现在只想演示如何保存时,为什么要使用3个按钮?为什么要使用嵌套布局?一个简单的vbox和treeTableView/输入字段/安全按钮就足够了)。事实上,我认为您自己很困惑:您似乎有两个PanesClass实例—一个用作调用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();
}
});
}
}
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 + "]";
}
}