Question

Hi I would like a class to be able to listen to one of its component's JButton. Here I have a Window class which has a MyPanel component which itself has JButtons. I would like Window to be notified when MyPanel's JButton is pressed. How can I do that?

Here is a piece of code, to use only for illustrating my need, it has not been tested.

Thank you for your help!

 class Window extends JFrame {
            private MyPanel myPane;

            public static void main(String [] args) {
                Window mainWindow = new Window();
    }

    public Window() {

        this.myPane = new MyPanel();
            getContentPane().add(myPane);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 600);
        this.setVisible(true);
    }

        public computations() {
    // Here I would like to get myPane.s, or do other things regarding myPane's attributes (with getters), only once myPane.b2 is pressed.
        }
}

class MyPanel extends JPanel {
    String s;
    JButton b1;
    JButton b2;

    public MyPanel() {
        s = new String("");
        b1 = new JButton("say hello");
        b2 = new JButton("Close");
        this.add(b1);
        this.add(b2);
        ButtonHandler phandler = new ButtonHandler();
        b1.addActionListener(phandler);
        b2.addActionListener( actionPerformed(ActionEvent e) {
            this.s = "Hello world";
        });

    }


    class ButtonHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // Tells Window class something happened.
        }
    }

}
Was it helpful?

Solution

I have a promotion for you, you can try if it works. Since I just set up my computer and has no java environment or IDE installed yet, sorry for cannot test my solution.

The main point is that you can pass an instance of Window to MyPanel, like the code bellow:

 class Window extends JFrame {

        public String myStr = "";
        private MyPanel myPane;

        public static void main(String [] args) {
            Window mainWindow = new Window();
}

public Window() {
    this.myPane = new MyPanel();
        getContentPane().add(myPane);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(300, 600);
    this.setVisible(true);
}

    public computations() {
// Here I would like to get myPane.s, or do other things regarding myPane's attributes (with getters), only once myPane.b2 is pressed.
     System.out.println(this.myStr);
    }
}

class MyPanel extends JPanel {
Window win = null;
String s;
JButton b1;
JButton b2;

public MyPanel(Window window) {
    this.win = window;
    s = new String("");
    b1 = new JButton("say hello");
    b2 = new JButton("Close");
    this.add(b1);
    this.add(b2);
    ButtonHandler phandler = new ButtonHandler();
    b1.addActionListener(phandler);
    b2.addActionListener( actionPerformed(ActionEvent e) {
        this.s = "Hello world";
    });

}


class ButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // Tells Window class something happened.
        this.win.myStr = s;
    }
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top