Search code examples
javaswingactionlistener

ActionListener doesn't work from other class


Here I have test application, where I can't understand why ActionListener from Test class doesn't work. Each help will make me happy. Thanks :) Here's code;

package com.company;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame {
Main(){
    GUI m = new GUI();
    this.getContentPane().add(m.panel);
    this.setDefaultCloseOperation(3);
    this.setBounds(0,0,400,200);
    this.setLocationRelativeTo(null);
    this.requestFocus();
    this.setVisible(true);
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Main();
            new Test();
        }
    });
}
}

class GUI{
GUI(){
    initComp();
    btn();
}
JPanel panel = new JPanel();
JButton button1;
JTextField textField;
private void initComp(){
    textField = new JTextField(10);
    button1 = new JButton("TEST");
    panel.add(textField);
    panel.add(button1);
}
public String getEnteredText(){
    return textField.getText().trim(); //trim dodatkowy
}

public void btn(){
    button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            System.out.println("Test from GUI-class");
        }
    });
    }
  }

class Test{
Test(){
    buttonGetter();

}
GUI m = new GUI();
String kol;
public void buttonGetter(){
    m.button1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            System.out.println("Test from Test-class");
        }
    });
}
}

Solution

  • Test doesn't add anything to any components/windows, so it's never visible on the screen, only the instance of GUI, which is created in Main is visible.

    A possibly better solution would be to use class inheritance to extend the GUI and override the btn method in order to provide your custom implementation, which might look something like...

    public class Test extends GUI {
    
        @Override
        public void btn() {
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(final ActionEvent e) {
                    System.out.println("Test from Test-class");
                }
            });
        }
    }
    

    Then instead of creating an instance of GUI in the Main method, you create an instance of Test instead, for example;

    GUI m = new Test();
    

    Runnable example

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class Main extends JFrame {
    
        Main() {
            GUI m = new Test();
            this.getContentPane().add(m.panel);
            this.setDefaultCloseOperation(3);
            this.setBounds(0, 0, 400, 200);
            this.setLocationRelativeTo(null);
            this.requestFocus();
            this.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Main();
                }
            });
        }
    
        public class GUI {
    
            GUI() {
                initComp();
                btn();
            }
            JPanel panel = new JPanel();
            JButton button1;
            JTextField textField;
    
            private void initComp() {
                textField = new JTextField(10);
                button1 = new JButton("TEST");
                panel.add(textField);
                panel.add(button1);
            }
    
            public String getEnteredText() {
                return textField.getText().trim(); //trim dodatkowy
            }
    
            public void btn() {
                button1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        System.out.println("Test from GUI-class");
                    }
                });
            }
        }
    
        public class Test extends GUI {
    
            @Override
            public void btn() {
                button1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        System.out.println("Test from Test-class");
                    }
                });
            }
        }
    }