Java “添加”和“删除”仅影响表而不影响列表我应该如何使其影响列表

Java “添加”和“删除”仅影响表而不影响列表我应该如何使其影响列表,java,javafx,arraylist,tableview,scenebuilder,Java,Javafx,Arraylist,Tableview,Scenebuilder,因此,我在DocTableController中添加和删除函数,以在GUI中编辑表。我将ArrayList从主控制器链接到DocTableController。 当我删除并添加该项时,它似乎仅对表有效,而对列表无效。一旦我返回主菜单并再次返回可修改场景,列表将重新初始化。当我再次进入tableView时,不会改变 主控制器 public class FXMLdocumentController implements Initializable { @FXML Label date;

因此,我在DocTableController中添加和删除函数,以在GUI中编辑表。我将ArrayList从主控制器链接到DocTableController。 当我删除并添加该项时,它似乎仅对表有效,而对列表无效。一旦我返回主菜单并再次返回可修改场景,列表将重新初始化。当我再次进入tableView时,不会改变

主控制器

public class FXMLdocumentController implements Initializable {
    @FXML Label date;
    @FXML Label time;
        
    //define static so in memory only 1 list
    static ArrayList<Doctor> doctor = new ArrayList<Doctor>();
    static int counter = 0;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //initialise all the list
        if (counter == 0){
            doctor.add(new Doctor("518", "Lew Wei Li", "Surgeon", "10PM-3AM", "MBBS,MD","Male",8000 , 88));
            doctor.add(new Doctor("101", "Tom Wong", "Obstetrician", "8AM-11AM", "MBBS,MS","Male",10000 ,11));
            doctor.add(new Doctor("102", "John Lim", "Physician", "7AM-11AM", "MBBS,MS","Male",7000 ,45));
            doctor.add(new Doctor("103", "Amy Chia", "Pediatrician", "6PM-11PM", "MBBS,MD","Female",6000, 8));
            doctor.add(new Doctor("104", "Chris Hemsworth", "Neurologist", "6PM-11PM", "BDM", "Female",8000,12));
            counter++;
        }
        
        //when the program start get the local computer time and date and display
        Calendar calendar = Calendar.getInstance();
        date.setText("Date: " + calendar.get(Calendar.DATE) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/"
                + calendar.get(Calendar.YEAR));
        time.setText("Time: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":"
                + calendar.get(Calendar.SECOND));
    }
    

    
    @FXML
    public void changeScreenDoc(ActionEvent event) throws IOException {//change to doctor session scene
        FXMLLoader loader  = new FXMLLoader();
        loader.setLocation(getClass().getResource("/Gui/DocTable.fxml"));//get the DocTable gui file
        Parent DocParent = loader.load();
        Scene DocScene = new Scene(DocParent);
        
        //get the data to link to other controller
        DocTablecontroller controller = loader.getController();
        controller.setDoc(doctor);
        
        Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
        window.setScene(DocScene);
        window.show();
    }

}
public class DocTablecontroller implements Initializable {
    //configure table
    @FXML private TableView<Doctor> Doctable;
    @FXML private TableColumn<Doctor, String> Idcol;
    @FXML private TableColumn<Doctor, String> Namecol;
    @FXML private TableColumn<Doctor, String> Speccol;
    @FXML private TableColumn<Doctor, String> Worktimecol;
    @FXML private TableColumn<Doctor, String> Quacol;
    @FXML private TableColumn<Doctor, Integer> RoomNumbercol;
    
    //Instance variable to create object
    @FXML private TextField newId;
    @FXML private TextField newName;
    @FXML private TextField newSpecialist;
    @FXML private TextField newWorktime;
    @FXML private TextField newQual;
    @FXML private TextField newRnum;
    
    //for alert user usage
    Alert a = new Alert(AlertType.WARNING); 
    
    //link the data from main controller
    public void setDoc(ArrayList<Doctor> doclist)
    {
        Doctable.getItems().addAll(doclist);
    }
    
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        
        //setting up the table and tell it where to find the value in the class by ("xx")
        Idcol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("id"));
        Namecol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("name"));
        Speccol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("specialist"));
        Worktimecol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("workTime"));
        Quacol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("qualification"));
        RoomNumbercol.setCellValueFactory(new PropertyValueFactory<Doctor,Integer>("roomNum"));
        
        //Doctable.setItems(Doclist);//injecting the observable list to the table 
        
        //set the table cell to be editable
        Doctable.setEditable(true);
        Idcol.setCellFactory(TextFieldTableCell.forTableColumn());
        Namecol.setCellFactory(TextFieldTableCell.forTableColumn());
        Speccol.setCellFactory(TextFieldTableCell.forTableColumn());
        Worktimecol.setCellFactory(TextFieldTableCell.forTableColumn());
        Quacol.setCellFactory(TextFieldTableCell.forTableColumn());
        RoomNumbercol.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
    }
    
    @FXML
    public void newDoctor() {//add new data(row) in the table(add button)
        Doctor newDoctor = new Doctor();
        boolean flag = true;
        //format checking
        if (!(newDoctor.setId(newId.getText()))){
             newId.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setName(newName.getText()))){
             newName.setText("Wrong format");
        }
        if (!(newDoctor.setSpecialist(newSpecialist.getText()))){
             newSpecialist.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setWorkTime(newWorktime.getText()))){
             newWorktime.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setQualification(newQual.getText()))){
             newQual.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setRoomNum(newRnum.getText()))){
             newRnum.setText("Wrong format");
             flag = false;
        }
        //if the format all correct allow to add new input else alert user some input format is wrong
        if(flag)
        {
            Doctable.getItems().add(newDoctor);
            
        }
        else 
        {
            a.setTitle("Input alert");
            a.setContentText("Input format wrong! please check again");
            a.showAndWait();
        }
    }
    
    //edit cell when a cell is double clicked
    public void changeCellEvent(CellEditEvent<?, ?> edittedcell)
    {
        //get the selected doctor data
        Doctor docSeleted = Doctable.getSelectionModel().getSelectedItem();
        //gone through format checking
        if(!(docSeleted.setId(edittedcell.getNewValue().toString())))
        {
            //alert if pattern not match
            a.setTitle("Input alert");
            a.setContentText("Input format wrong! please check again");
            a.showAndWait();
        }
        Doctable.refresh();//if no this then the value of edited row will stay as the user input string 
    }
    
    
    //delete user selected cell
    public void dlt() {
            Doctable.getItems().removeAll(Doctable.getSelectionModel().getSelectedItems());
    }
    
    
    @FXML//change scene(main menu button)
    public void BackMenu(ActionEvent event) throws IOException {
        FXMLLoader loader  = new FXMLLoader();
        loader.setLocation(getClass().getResource("/Gui/Menu.fxml"));
        Parent MenuParent = loader.load();
        
        Scene MenuScene = new Scene(MenuParent);
        
        Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
        window.setScene(MenuScene);
        window.show();
    }

    
}
公共类FXMLdocumentController实现可初始化{
@FXML标签日期;
@FXML标记时间;
//定义静态,以便在内存中仅显示1个列表
静态ArrayList医生=新建ArrayList();
静态整数计数器=0;
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
//初始化所有列表
如果(计数器==0){
新增医生(新医生(“518”,“刘伟力”,“外科医生”,“晚上10点至凌晨3点”,“医学博士”,“男性”,8000,88));
新增医生(新医生(“101”,“Tom Wong”,“产科医生”,“上午8点至11点”,“MBBS,MS”,“男性”,10000,11));
新增医生(新医生(“102”、“John Lim”、“内科医生”、“上午7点至11点”、“MBBS、MS”、“男性”,7000、45));
新增医生(新医生(“103”,“Amy Chia”,“儿科医生”,“下午6点至11点”,“医学博士”,“女性”,6000,8));
新增医生(新医生(“104”、“Chris Hemsworth”、“神经科医生”、“下午6点至11点”、“BDM”、“女性”,8000,12));
计数器++;
}
//当程序启动时,获取本地计算机的时间和日期并显示
日历=Calendar.getInstance();
date.setText(“日期:+calendar.get(calendar.date)+“/”+(calendar.get(calendar.MONTH)+1)+“/”
+calendar.get(calendar.YEAR));
time.setText(“时间:+calendar.get(calendar.HOUR)++”:“+calendar.get(calendar.MINUTE)+”:”
+calendar.get(calendar.SECOND));
}
@FXML
public void changeScreenDoc(ActionEvent事件)引发IOException{//对医生会话场景的更改
FXMLLoader=新的FXMLLoader();
loader.setLocation(getClass().getResource(“/Gui/DocTable.fxml”);//获取可修改的Gui文件
Parent DocParent=loader.load();
场景DocScene=新场景(DocParent);
//获取要链接到其他控制器的数据
DocTablecontroller=loader.getController();
控制器.setDoc(医生);
阶段窗口=(阶段)((节点)事件.getSource()).getScene().getWindow();
window.setScene(DocScene);
window.show();
}
}
可修改的场景控制器

public class FXMLdocumentController implements Initializable {
    @FXML Label date;
    @FXML Label time;
        
    //define static so in memory only 1 list
    static ArrayList<Doctor> doctor = new ArrayList<Doctor>();
    static int counter = 0;
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //initialise all the list
        if (counter == 0){
            doctor.add(new Doctor("518", "Lew Wei Li", "Surgeon", "10PM-3AM", "MBBS,MD","Male",8000 , 88));
            doctor.add(new Doctor("101", "Tom Wong", "Obstetrician", "8AM-11AM", "MBBS,MS","Male",10000 ,11));
            doctor.add(new Doctor("102", "John Lim", "Physician", "7AM-11AM", "MBBS,MS","Male",7000 ,45));
            doctor.add(new Doctor("103", "Amy Chia", "Pediatrician", "6PM-11PM", "MBBS,MD","Female",6000, 8));
            doctor.add(new Doctor("104", "Chris Hemsworth", "Neurologist", "6PM-11PM", "BDM", "Female",8000,12));
            counter++;
        }
        
        //when the program start get the local computer time and date and display
        Calendar calendar = Calendar.getInstance();
        date.setText("Date: " + calendar.get(Calendar.DATE) + "/" + (calendar.get(Calendar.MONTH) + 1) + "/"
                + calendar.get(Calendar.YEAR));
        time.setText("Time: " + calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":"
                + calendar.get(Calendar.SECOND));
    }
    

    
    @FXML
    public void changeScreenDoc(ActionEvent event) throws IOException {//change to doctor session scene
        FXMLLoader loader  = new FXMLLoader();
        loader.setLocation(getClass().getResource("/Gui/DocTable.fxml"));//get the DocTable gui file
        Parent DocParent = loader.load();
        Scene DocScene = new Scene(DocParent);
        
        //get the data to link to other controller
        DocTablecontroller controller = loader.getController();
        controller.setDoc(doctor);
        
        Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
        window.setScene(DocScene);
        window.show();
    }

}
public class DocTablecontroller implements Initializable {
    //configure table
    @FXML private TableView<Doctor> Doctable;
    @FXML private TableColumn<Doctor, String> Idcol;
    @FXML private TableColumn<Doctor, String> Namecol;
    @FXML private TableColumn<Doctor, String> Speccol;
    @FXML private TableColumn<Doctor, String> Worktimecol;
    @FXML private TableColumn<Doctor, String> Quacol;
    @FXML private TableColumn<Doctor, Integer> RoomNumbercol;
    
    //Instance variable to create object
    @FXML private TextField newId;
    @FXML private TextField newName;
    @FXML private TextField newSpecialist;
    @FXML private TextField newWorktime;
    @FXML private TextField newQual;
    @FXML private TextField newRnum;
    
    //for alert user usage
    Alert a = new Alert(AlertType.WARNING); 
    
    //link the data from main controller
    public void setDoc(ArrayList<Doctor> doclist)
    {
        Doctable.getItems().addAll(doclist);
    }
    
    
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        
        //setting up the table and tell it where to find the value in the class by ("xx")
        Idcol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("id"));
        Namecol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("name"));
        Speccol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("specialist"));
        Worktimecol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("workTime"));
        Quacol.setCellValueFactory(new PropertyValueFactory<Doctor,String>("qualification"));
        RoomNumbercol.setCellValueFactory(new PropertyValueFactory<Doctor,Integer>("roomNum"));
        
        //Doctable.setItems(Doclist);//injecting the observable list to the table 
        
        //set the table cell to be editable
        Doctable.setEditable(true);
        Idcol.setCellFactory(TextFieldTableCell.forTableColumn());
        Namecol.setCellFactory(TextFieldTableCell.forTableColumn());
        Speccol.setCellFactory(TextFieldTableCell.forTableColumn());
        Worktimecol.setCellFactory(TextFieldTableCell.forTableColumn());
        Quacol.setCellFactory(TextFieldTableCell.forTableColumn());
        RoomNumbercol.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
    }
    
    @FXML
    public void newDoctor() {//add new data(row) in the table(add button)
        Doctor newDoctor = new Doctor();
        boolean flag = true;
        //format checking
        if (!(newDoctor.setId(newId.getText()))){
             newId.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setName(newName.getText()))){
             newName.setText("Wrong format");
        }
        if (!(newDoctor.setSpecialist(newSpecialist.getText()))){
             newSpecialist.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setWorkTime(newWorktime.getText()))){
             newWorktime.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setQualification(newQual.getText()))){
             newQual.setText("Wrong format");
             flag = false;
        }
        if (!(newDoctor.setRoomNum(newRnum.getText()))){
             newRnum.setText("Wrong format");
             flag = false;
        }
        //if the format all correct allow to add new input else alert user some input format is wrong
        if(flag)
        {
            Doctable.getItems().add(newDoctor);
            
        }
        else 
        {
            a.setTitle("Input alert");
            a.setContentText("Input format wrong! please check again");
            a.showAndWait();
        }
    }
    
    //edit cell when a cell is double clicked
    public void changeCellEvent(CellEditEvent<?, ?> edittedcell)
    {
        //get the selected doctor data
        Doctor docSeleted = Doctable.getSelectionModel().getSelectedItem();
        //gone through format checking
        if(!(docSeleted.setId(edittedcell.getNewValue().toString())))
        {
            //alert if pattern not match
            a.setTitle("Input alert");
            a.setContentText("Input format wrong! please check again");
            a.showAndWait();
        }
        Doctable.refresh();//if no this then the value of edited row will stay as the user input string 
    }
    
    
    //delete user selected cell
    public void dlt() {
            Doctable.getItems().removeAll(Doctable.getSelectionModel().getSelectedItems());
    }
    
    
    @FXML//change scene(main menu button)
    public void BackMenu(ActionEvent event) throws IOException {
        FXMLLoader loader  = new FXMLLoader();
        loader.setLocation(getClass().getResource("/Gui/Menu.fxml"));
        Parent MenuParent = loader.load();
        
        Scene MenuScene = new Scene(MenuParent);
        
        Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
        window.setScene(MenuScene);
        window.show();
    }

    
}
公共类DocTablecontroller实现可初始化{
//配置表
@FXML私有表视图可修改;
@FXML私有表列Idcol;
@FXML私有表列名称col;
@FXML私有表列Speccol;
@FXML私有表列Worktimecol;
@FXML专用表列Quacol;
@FXML私有表列RoomNumbercol;
//实例变量来创建对象
@FXML私有文本字段newId;
@FXML私有文本字段newName;
@FXML私有文本字段新闻专家;
@FXML私有文本字段newWorktime;
@FXML私有文本字段newQual;
@FXML私有文本字段newRnum;
//用于提醒用户使用
警报a=新警报(AlertType.WARNING);
//从主控制器链接数据
公共作废setDoc(ArrayList doclist)
{
Doctable.getItems().addAll(doclist);
}
@凌驾
公共void初始化(URL位置、ResourceBundle资源){
//设置表格,并通过(“xx”)告诉它在类中的何处查找值
Idcol.setCellValueFactory(新属性ValueFactory(“id”);
Namecol.setCellValueFactory(新属性ValueFactory(“名称”);
Speccol.setCellValueFactory(新财产价值工厂(“专家”);
Worktimecol.setCellValueFactory(新属性ValueFactory(“workTime”));
Quacol.setCellValueFactory(新财产价值工厂(“资格”);
RoomNumbercol.setCellValueFactory(新属性ValueFactory(“roomNum”));
//Doctable.setItems(Doclist);//将可观察列表注入到表中
//将表格单元格设置为可编辑
可修改。可设置可编辑(true);
Idcol.setCellFactory(TextFieldTableCell.forTableColumn());
Namecol.setCellFactory(TextFieldTableCell.forTableColumn());
Speccol.setCellFactory(TextFieldTableCell.forTableColumn());
Worktimecol.setCellFactory(TextFieldTableCell.forTableColumn());
Quacol.setCellFactory(TextFieldTableCell.forTableColumn());
RoomNumbercol.setCellFactory(TextFieldTableCell.forTableColumn(新的IntegerStringConverter());
}
@FXML
public void newDoctor(){//在表中添加新数据(行)(添加按钮)
新医生=新医生();
布尔标志=真;
//格式检查
if(!(newDoctor.setId(newId.getText())){
newId.setText(“格式错误”);
flag=false;
}
if(!(newDoctor.setName(newName.getText())){
newName.setText(“格式错误”);
}
if(!(newDoctor.setSpecialist(newSpecialist.getText())){
newSpecialist.setText(“错误格式”);
flag=false;
}
if(!(newDoctor.setWorkTime(newWorktime.getText())){
newWorktime.setText(“错误格式”);
flag=false;
}
if(!(newDoctor.setQualification(newQual.getText())){
newQual.setText(“错误格式”);
flag=false;
}
if(!(newDoctor.setRoomNum(newRnum.getText())){
newRnum.setText(“错误格式”);
flag=false;
}
//如果格式都正确,则允许添加新输入,否则会提醒用户某些输入格式错误
国际单项体育联合会(旗)