创建一个编辑窗口,从以前的窗口ListView中提取信息

时间:2016-10-26 03:26:53

标签: java arrays user-interface

我是编程新手(6个月左右)。我正在为JavaFX中的基本应用程序提供有趣和GUI体验。我目前正在寻找一种方法来打开"查看/编辑帐户"屏幕。我是一个上一个窗口,我有一个列表视图框,显示我在一个arraylist中的帐户名称(我使用文本文件作为一种保存方式,因为我还没有冒险进入SQL)。目标是能够单击数组对象的名称,点击编辑,然后新窗口打开一些GUI,其中包含有关您刚刚单击的对象的更详细信息,甚至允许您编辑变量。我目前利用javaFX内置的selectionmode方法将我点击的objext加载到person变量中,我只是不知道如何将其转移到新的对话窗口。这是我的一些代码(是listView windows控制器)p.s.如果它草率,我道歉。我有很多试验和错误:

public class accountController {


public List<accountObj> myList;
@FXML
private ListView<accountObj> test;

@FXML
AnchorPane newAccountPane;

public void  initialize () { //initializes the code. Seems similar to a main class
    test.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<accountObj>() {//adds a listener to update the info of what is selected
        @Override
        public void changed(ObservableValue<? extends accountObj> observable, accountObj oldValue, accountObj newValue) {
            if (newValue != null) {//means if something is selected then it pulls in the info of what is selected in the list
                accountObj person = test.getSelectionModel().getSelectedItem();


            }
        }
    });

    test.setItems(DataTwo.getInstanceTwo().getAccountObjs());
    test.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    test.getSelectionModel().selectFirst();
}
    @FXML
    public void handleClicktest () {
        accountObj person = (accountObj) test.getSelectionModel().getSelectedItem();



    }
        public void showViewAccount() {//shows the new account screen.
            Dialog<ButtonType> dialog2 = new Dialog<>();
            dialog2.initOwner(newAccountPane.getScene().getWindow());
            FXMLLoader fxmlLoader2 = new FXMLLoader();
            fxmlLoader2.setLocation(getClass().getResource("viewAccount.fxml"));
            try {
                dialog2.getDialogPane().setContent(fxmlLoader2.load());
            } catch (IOException e) {
                System.out.println("Couldnt load the dialog");
                e.printStackTrace();
                return;
            }

            dialog2.getDialogPane().getButtonTypes().add(ButtonType.OK);//these add the ok and cancel buttons to the window
            dialog2.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);

            Optional<ButtonType> result = dialog2.showAndWait();
            if (result.isPresent() && result.get() == ButtonType.OK) {
                viewAccountController controller = fxmlLoader2.getController();


            }}


        public void showNewAccount() {//shows the new account screen.
            Dialog<ButtonType> dialog = new Dialog<>();
            dialog.initOwner(newAccountPane.getScene().getWindow());
            FXMLLoader fxmlLoader = new FXMLLoader();
            fxmlLoader.setLocation(getClass().getResource("newAccount.fxml"));
            try {
                dialog.getDialogPane().setContent(fxmlLoader.load());
            } catch (IOException e) {
                System.out.println("Couldnt load the dialog");
                e.printStackTrace();
                return;
            }

            dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);//these add the ok and cancel buttons to the window
            dialog.getDialogPane().getButtonTypes().add(ButtonType.CANCEL);
            Optional<ButtonType> result = dialog.showAndWait();
            if (result.isPresent() && result.get() == ButtonType.OK) {
                newAccountController controller = fxmlLoader.getController();
                accountObj newPerson=controller.processResults2();
                test.getSelectionModel().select(newPerson);
            }
        }

1 个答案:

答案 0 :(得分:0)

showViewAccount方法的声明更改为

public void showViewAccount(accountObj person)

接下来,在handleClicktest方法的正文中,您可以将person参数传递给showViewAccount方法 看起来应该是这样的

public void handleClicktest () {
    accountObj person = (accountObj) test.getSelectionModel().getSelectedItem();

    showViewAccount(person);
}