java MouseListener and MouseAdapter - pass in variable

java, listener

Solution

I can't access any variables from inside the class that are outside.

Not in general, but you can access final variables, so just put `final` in front of the `String message` parameter:

public static void createDialog(Button b, final String message) {
    //                                    ^^^^^
    MouseListener mouseListener = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
                JOptionPane.showConfirmDialog(null,
                        message, message, JOptionPane.YES_NO_OPTION);
            }
        }
    };
}

Problem

i have the following method: ``` public static void createDialog(Button b, String message) { MouseListener mouseListener = new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { if (SwingUtilities.isLeftMouseButton(mouseEvent)) { JOptionPane.showConfirmDialog(null, "mymessage", "mymessage", JOptionPane.YES_NO_OPTION); } } }; } ``` I want to be able to get the parameter message, into the JOptionPane where is says mymessage. Is there a way to do this? I can't access any variables from inside the class that are outside. Is there a way to get that value in there? I would eventually like to get a few other parameters in there as well. Thanks!

Original source