Search code examples
javaswingjframejbuttonactionlistener

Main has to be astract, ActionPerformed method problem with Listener


This program is just for the sake of teaching myself Java. While coding around i ran into the following problem: I'm getting errors (red underlining) when using a listener for a button which I implement in my Main class.

Since I'm new in learning Java please excuse if the solution is obvious.I already tried making both the main and the actionPerfomed method abstract but that lead to further issues. I also tried @Override before the actionPerformed method.

Here is the code:

// Java program to create a blank text
// field of definite number of columns.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Main extends JFrame implements ActionListener {
    // JTextField
    static JTextField t;

    // JFrame
    static JFrame f;

    // JButton
    static JButton b;

    // label to diaplay text
    static JLabel l;

    // default constructor
    Main()
    {
    }

    // main class
    public static void main(String[] args)
    {
        // create a new frame to stor text field and button
        f = new JFrame("textfield");

        // create a label to display text
        l = new JLabel("nothing entered");

        // create a new button
        b = new JButton("submit");

        // create a object of the text class
        Main te = new Main();

        // addActionListener to button
        b.addActionListener(te);

        // create a object of JTextField with 16 columns
        t = new JTextField(16);

        // create a panel to add buttons and textfield
        JPanel p = new JPanel();

        // add buttons and textfield to panel
        p.add(t);
        p.add(b);
        p.add(l);

        l.setOpaque(true);
        // add panel to frame
        f.add(p);

        // set the size of frame
        f.setSize(300, 300);

        p.setBackground(Color.cyan);

        f.show();
    }

    // if the button is pressed
    public void actionPerformed(java.awt.event.ActionEvent e, JPanel p)
    {
        String s = e.getActionCommand();
        if (s.equals("submit")) {
            // set the text of the label to the text of the field
            if(t.getText().equals("hue")) {

                p.setBackground(changeColor());
            }
            l.setText(t.getText());

            // set the text of field to blank
            t.setText(" ");
        }
    }
    public Color changeColor() {
        int r = (int)(Math.random())*256;
        int g = (int)(Math.random())*256;
        int b = (int)(Math.random())*256;
        Color color = new Color(r,g,b);
        return color;
    }
}

Solution

  • Here:

    public void actionPerformed(java.awt.event.ActionEvent e, JPanel p)
    

    Should be

    public void actionPerformed(java.awt.event.ActionEvent e)
    

    You have to match the expected signature exactly! You can't just add more parameters to a method you are supposed to override!

    You see, in the end, it is not your code that will invoke that method when a button is clicked. It is the Swing framework that will do it. You tell it: when an action happens on this thing, then call back my code. How do you expect that this framework could know that you want an additional parameter to be passed?!

    Beyond that: make it your practice to put @Override in front of any method that you expect to override a method (like in this case, where you are implementing a method of that interface). Because then the compiler can tell you when you make such mistakes!

    But of course, you added that parameter so that the listener can use it. So: make it known to the listener, for example by adding a field to your Main class that you initialize in the constructor! So instead of passing the panel to that one method (where it can't go) pass it when doing new for your Main instance.