首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要实现CallBack方法

需要实现CallBack方法
EN

Stack Overflow用户
提问于 2018-09-13 17:15:42
回答 2查看 43关注 0票数 0

我有一个根屏幕,生成一个弹出窗口,在弹出窗口中,我有一个带有按钮的列表视图,我想要更新根屏幕中的文本域,并在弹出窗口中单击按钮时关闭弹出窗口。popup及其控制器的代码。

弹出窗口

代码语言:javascript
复制
public void display() throws IOException {
Stage window =new Stage();
        FXMLLoader loader=new FXMLLoader();
        Parent root = loader.load(getClass().getResource("/ProfilePopup.fxml"));
        window.setTitle("Your profile");
        window.setScene(new Scene(root, 400, 500));
        window.show();


    }

PopUPController

代码语言:javascript
复制
public void initialize() {
        listView.setEditable(true);

        listView.setVisible(true);
        listView.setItems(walletModel.myWallets);

        listView.setCellFactory(param -> {
            try {
                return new EditableCell();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        });
        listView.layout();


        addWalletButton.setOnMouseClicked(event -> {
            walletModel.createWallet();
            listView.getFixedCellSize();
            size.setText("Total Wallets:  " + walletModel.walletSize());
        });
        if (walletModel.myWallets.size() == 0) {
            walletModel.initializeWalletData();
            walletModel.myWallets.add(walletModel.initializeWalletData());
        }
        size.setText("Wallet Size " + walletModel.walletSize());
    }
    static class EditableCell extends ListCell<WalletModel.WalletData> {

        private final WalletCellController controller;


        EditableCell() throws IOException {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/selectButton.fxml"));
            Node graphic = loader.load();
            controller = loader.getController();
            setGraphic(graphic);

        }

        @Override
        protected void updateItem(WalletModel.WalletData item, boolean empty) {


            if (empty) {

                controller.rootView.setVisible(false);

            } else {
                controller.textField.setText(item.getName());
                controller.rootView.setVisible(true);


            }
        }
    }


} 

我想在listview上的按钮更新根屏幕时,它被点击,并加上关闭弹出窗口以及。每个listview都从下面的walletcellcontroller代码中获取图形。下面是我在根屏幕上调用的方法。在根屏幕中创建实例,然后调用(Popup popup=new Popup();)

代码语言:javascript
复制
public void popupOpen() throws IOException {
        popup.display();


    }

下面是listview项的代码

代码语言:javascript
复制
public class WalletCellController implements OnClick {
    public Button select;
    public TextField textField;
    public AnchorPane rootView;
    public  void initialize(){
        onMouseClicked();
    }
    public void onMouseClicked() {

        select.setOnAction(closeEvent -> {
            Node source = (Node) closeEvent.getSource();
            Stage stage = (Stage) source.getScene().getWindow();
            stage.close();
        });



}} 

你能告诉我如何在这里使用actionevents事件的回调函数吗?我想我需要从弹出控制器回调到POPUP,然后再从POPup回调到根屏幕。我是个java新手,所以我不太确定它的实现。

EN

回答 2

Stack Overflow用户

发布于 2018-09-13 17:32:21

代码语言:javascript
复制
interface Callable {
public void callBackMethod();
}

class Worker {
// Worker gets a handle to the boss object via the Callable interface.
// There's no way this worker class can call any other method other than
// the one in Callable.
public void doSomeWork(Callable myBoss) {
    myBoss.callBackMethod();
    // ERROR!
    //myBoss.directMethod();
}
}

class Boss implements Callable {
public Boss() {
    // Boss creates a worker object, and tells it to do some work.
    Worker w1 = new Worker();
    // Notice, we're passing a reference of the boss to the worker.
    w1.doSomeWork(this);
}
//developer that develop library just call controll the place of calling

public void callBackMethod() {
    System.out.println("What do you want?");
}

public void directMethod() {
    System.out.println("I'm out for coffee.");
}
}

public class Main {

public static void main(String[] args) {

    Boss b = new Boss();
    b.directMethod();

    // write your code here
}
}

这是call back method的示例代码

票数 0
EN

Stack Overflow用户

发布于 2018-09-13 21:57:46

在这种情况下,我建议使用Dialog,因为它允许您查询和等待用户输入。

示例

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Dialog?>
<?import javafx.scene.control.DialogPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>

<Dialog xmlns:fx="http://javafx.com/fxml/1" fx:id="dialog" fx:controller="fxml.DialogController">
    <dialogPane>
        <DialogPane headerText="Choose item!">
            <content>
                <VBox prefWidth="100" spacing="5">
                    <children>
                        <Button text="a" onAction="#choice" maxWidth="Infinity" />
                        <Button text="b" onAction="#choice" maxWidth="Infinity" />
                        <Button text="c" onAction="#choice" maxWidth="Infinity" />
                        <Button text="Cancel" onAction="#cancel" maxWidth="Infinity" />
                    </children>
                </VBox>
            </content>
        </DialogPane>
    </dialogPane>
</Dialog>
代码语言:javascript
复制
public class DialogController {
    @FXML
    private Dialog<String> dialog;

    @FXML
    private void choice(ActionEvent event) {
        Button source = (Button) event.getSource();
        dialog.setResult(source.getText());
        dialog.close();
    }
    
    @FXML
    private void cancel() {
        dialog.setResult("");
        dialog.close();
    }
    
}
代码语言:javascript
复制
@Override
public void start(Stage primaryStage) {
    TextField textField = new TextField();
    Button btn = new Button("Choose");
    btn.setOnAction((ActionEvent event) -> {
        Dialog<String> dialog;
        try {
            dialog = FXMLLoader.load(getClass().getResource("/fxml/Dialog.fxml"));
        } catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
        Optional<String> result = dialog.showAndWait();
        if (!result.orElse("").isEmpty()) {
            textField.setText(s);
        }
    });

    VBox root = new VBox(textField, btn);

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);
    primaryStage.show();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52310465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档