Search code examples
javaswingjbuttonactionlistener

Java how to call a method from a child class


I'm learning Swing to make GUI in java. My goal is to have 1 mainGUI class to initialize everything and another class that controls all the button.

What I'm doing now is I have a mainGUI which has:

public class mainGUI(){
.... (main and initialize things here) ....

protected JButton btnLogin;
public void initialize(){
    btnLogin = new JButton("Login");
    btnLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            _buttonLogin();
        }
    });
}

protected void _buttonLogin(){};
}

Then in my buttonControl I have:

public class buttonControl extends mainGUI{
@Override
protected void _buttonLogin(){
    if (isLogin == true){
        btnLogin.setEnabled(false); 
    } else {
        // somthing else
    }
}
}

The program actually works but not as i expected. When i click on the "login" button, the login button is not set to unclickable. If i don't have the _buttonLogin method in the mainGUI class then I cannot call it from buttonControl class.

I'm just wondering is my approach right in this situation? or any other neat way to have a separated listener class?

Thank you so much


Solution

  • If you need a Single class for control all the buttons you have you can create a ButtonControl class which can register and de-register buttons to it and handle its events inside the control class. A Simple example code is given

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    
    
    public class MainUI extends JFrame{
    
    ButtonController buttonController;
    
    public MainUI() {
        super();
        buttonController=new ButtonController(this);
        initialize();
    }
    
    private void initialize() {
    
        JTextField userName=new JTextField();
        JPasswordField passwordField=new JPasswordField();
    
        JButton loginButton=new JButton("Login");
        loginButton.setActionCommand(ButtonController.LOGIN_COMMAND);
    
        JButton cancelButton=new JButton("Cancel");
        cancelButton.setActionCommand(ButtonController.CANCEL_COMMAND);
    
        JPanel contentPane=new JPanel();
        contentPane.setLayout(new GridLayout(3,2));
        contentPane.add(new JLabel("Username : "));
        contentPane.add(userName);
        contentPane.add(new JLabel("Password : "));
        contentPane.add(passwordField);
        contentPane.add(loginButton);
        contentPane.add(cancelButton);
    
        buttonController.registerButton(loginButton);
        buttonController.registerButton(cancelButton);
    
        setContentPane(contentPane);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        pack();
    }
    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        MainUI ui=new MainUI();
        ui.setVisible(true);
    }
    
    
    }
    
    
    class ButtonController implements ActionListener
    {
    private MainUI mainUI;
    
    public static String LOGIN_COMMAND="Login";
    public static String CANCEL_COMMAND="Cancel";
    
    public ButtonController(MainUI mainUi ) {
        this.mainUI=mainUi;
    }
    
    public void registerButton(JButton button)
    {
        button.addActionListener(this);
    }
    
    public void deRegisterButton(Button button)
    {
        button.removeActionListener(this);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals(LOGIN_COMMAND))
        {
            ((JButton)e.getSource()).setEnabled(false);
        }
        if(e.getActionCommand().equals(CANCEL_COMMAND))
        {
            mainUI.dispose();
        }
    }
    
    }