如何从javafx的UI视图中删除按钮的实例 [英] How to delete the instance of the button from UI view in javafx
问题描述
我在UI的HBox中存在一个java fx中的按钮,并且我想在单击同一按钮时删除该按钮.我已经为该事件编写了动作事件:
I have a button in the java fx which is present in HBox of the UI and I want to delete that Button on clicking the same button.i have written the action event for the same:
@FXML
private HBOx projectlist;
String buttonid;
字符串buttoname; /** *初始化控制器类. */ @FXML 公共按钮删除;
String buttoname; /** * Initializes the controller class. */ @FXML public Button delete;
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
buttonid= button.getId();
buttoname = button.getText();
projectlist.getChildren().remove(buttoname);
}
});
实际上问题是我正在从该功能的其他控制器获取按钮,尽管我能够在delete动作事件中使用buttonid变量,但我无法在另一个button(Delete)动作事件中使用该按钮:public void setButton(Button button,String buttonname){
Actually the problem is I am getting the button from other controller in this function and i am not able to use that button in another button(Delete) action event although i am able to use buttonid variable in delete action event: public void setButton(Button button,String buttonname) {
projectlist.setSpacing(10);
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
buttonid= button.getId();
buttoname = button.getText();
}
});
projectlist.getChildren().add(button);
projectlist.getChildren().add(button);
} @FXML
private void gotoDelete(ActionEvent event) throws IOException{
ProjectModule.DeleteProject(Integer.parseInt(buttonid));
projectlist.getChildren().remove(button);
}
推荐答案
尝试
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
buttonid= button.getId();
buttoname = button.getText();
projectList.getChildren().remove(button);
}
});
这篇关于如何从javafx的UI视图中删除按钮的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!