javaswingjframeawtactionlistener

The JFrame or the JPanel don't listen to the clicking event


I have this JFrame(BorderLayout), that has 3 Jpanel in it(north, center, south). The center panel, has a login button. For testing purposes, what I want is to print "hello", when pressed.

This is how the frame currently looks: enter image description here

I have tried 2 ways:

  1. "implements ActionListener", on the center panel, but nothing happens, not even an error.

  2. "implements ActionListener", on the frame itself, however, I am having issues (more like, I don't know how to) "importing"/having access to the button, so the JFrame can listen to the cliking event

Hope you can help me.

This is the code:

Main program:

package com.system.reservation.airline;

public class Main {
    public static void main(String[] args) {
        LoginPage loginPage = new LoginPage(); 
    }
}

LoginPage:

import com.system.reservation.airline.panels.BodyPanel;
import com.system.reservation.airline.panels.FooterPanel;
import com.system.reservation.airline.panels.HeaderPanel;

import javax.swing.*;
import java.awt.*;

public class LoginPage extends JFrame{ 
    HeaderPanel headerPanel = new HeaderPanel();
    BodyPanel bodyPanel = new BodyPanel();
    FooterPanel footerPanel = new FooterPanel();

    public LoginPage(){ // Constructor
        this.setTitle("Login");
        this.setSize(400, 600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        this.getContentPane().setBackground(new Color(240,248,255));
        this.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        this.add(headerPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        this.add(bodyPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridheight = 3;
        this.add(footerPanel, gbc);

        this.pack();
        this.setVisible(true);
    }
}

BodyPanel:

package com.system.reservation.airline.panels;

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

public class BodyPanel extends JPanel implements ActionListener{
    JLabel login_label;
    JButton login_button = new JButton("Login");
    Color transparent = new Color(1f,0f,0f,0f);

    public BodyPanel(){
        this.setBackground(transparent);
        this.setLayout(new BorderLayout());
        this.setBorder(BorderFactory.createLineBorder(Color.green, 3));
        //------------ Login Panel ------------------------
        JPanel login_panel = new JPanel(new BorderLayout());
        login_panel.setBackground(transparent);
        login_panel.setPreferredSize(new Dimension(400, 100));
        //login_panel.setBorder(BorderFactory.createLineBorder(Color.YELLOW, 5));

        login_label = new JLabel();
        login_label.setText("Login");
        login_label.setHorizontalAlignment(JLabel.CENTER);
        login_label.setFont(new Font("Arial", Font.PLAIN, 20));

        login_panel.add(login_label, BorderLayout.NORTH);
        //------------ Login Panel ------------------------

        //------------ Input Panel ------------------------
        JPanel input_fields_panel = new JPanel(new GridBagLayout());
        input_fields_panel.setBackground(transparent);
        input_fields_panel.setPreferredSize(new Dimension(400, 150));
        //input_fields_panel.setBorder(BorderFactory.createLineBorder(Color.black, 5));

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.weightx = 1;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;

        //-------------
        JLabel userName_label = new JLabel("Username: ");
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = new Insets(0,10,0,0);
        input_fields_panel.add(userName_label, gbc);

        JTextField userName_txb = new JTextField();
        userName_txb.setPreferredSize(new Dimension(250,30));
        gbc.gridx = 3;
        gbc.insets = new Insets(0,0,5,20);
        input_fields_panel.add(userName_txb, gbc);
        //-------------

        //-------------
        JLabel password_label = new JLabel("Password: ");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;
        gbc.insets = new Insets(0,10,0,0);
        input_fields_panel.add(password_label, gbc);

        JTextField password_txb = new JTextField();
        password_txb.setPreferredSize(new Dimension(250,30));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 3;
        gbc.gridheight = 1;
        gbc.insets = new Insets(5,0,5,20);
        input_fields_panel.add(password_txb, gbc);
        //-------------

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbc.insets = new Insets(5,150,5,150);
        input_fields_panel.add(login_button, gbc);

        JLabel tokenResponse = new JLabel("Username or password, are incorrect!");
        tokenResponse.setHorizontalAlignment(JLabel.CENTER);
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 4;
        gbc.gridheight = 1;
        gbc.insets = new Insets(0,0,0,0);
        input_fields_panel.add(tokenResponse, gbc);
        //------------ Input Panel ------------------------

        //-------------------------------------
        this.add(login_panel, BorderLayout.NORTH);
        this.add(input_fields_panel, BorderLayout.CENTER);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == login_button) {
            System.out.println("Hello");
        }
    }
}

The center panel is the BodyPanel, hence I did not include the code for the HeaderPanel, or the FooterPanel.

So to recap, I want to either the LoginPage to have access or to listen to the clicking event of the login_button, or the exact same for the BodyPanel.


Solution

  • You forgot to add an implementation of the ActionListener interface to your button.

    The constructor should include something like this at some point:

    public BodyPanel(){
       //...
       login_button.addActionListener(this);
       //...
    }