Question

I had a huge file for creating this GUI and I have shortened it by using arrays. I am trying to add an ActionListener but when the arrays are being added and I am getting the following errors:

/tmp/jc_3531/GUI.java:60: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (GUI)
            nButtons[c].addActionListener( this );
                       ^
/tmp/jc_3531/GUI.java:69: cannot find symbol
symbol  : method addActionListener(GUI)
location: class javax.swing.JLabel
            labels[c].addActionListener( this );
                     ^
/tmp/jc_3531/GUI.java:78: addActionListener(java.awt.event.ActionListener) in javax.swing.JTextField cannot be applied to (GUI)
            fields[c].addActionListener( this );
                     ^
/tmp/jc_3531/GUI.java:90: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (GUI)
            sButtons[c].addActionListener( this );
                       ^
4 errors

What am I doing wrong? Is there an easier way to self diagnose (like an editor with an integrated syntax guide)?

Here is my Code

// GUI with navigation amd file manipulation buttons
import javax.swing.*; // provides basic window features
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.text.NumberFormat;

public class GUI extends JFrame 
{
    private Inventory inv;      
    private int currentDisplay = 0; 

    private JPanel nButtonJPanel;
    private String nNames[]={"Load","Add","Modify","Delete","Search","Save"};
    private JButton nButtons[];

    private JPanel labelJPanel;
    private String labeled[]={"Item ID","Name","Rating","# in Stock","Price","Inventory Value",
    "Restocking Fee (5%)","Inventory Value with Fee","Total Value with Fee (all DVD's)"};
    private JLabel labels[];

    private JPanel fieldJPanel;
    private String fieldsBlank[]={"","","","","","","","",""};
    private JTextField fields[];

    private JPanel sButtonJPanel;
    private String sNames[]={"First","Prev","","Next","Last"};
    private JButton sButtons[];

   // LabelFrame constructor adds JLabels to JFrame
   public GUI()
   {
      super( "Inventory Program v.5" );
      setLayout( new BorderLayout() ); // set frame layout

        // initialize values
        RatedDVD d1 = new RatedDVD(1, "One Flew Over The Cuckoo's Nest", 3, 9.99, "PG");
        RatedDVD d2 = new   RatedDVD(2, "The Matrix", 1, 13.01, "PG13");
        RatedDVD d3 = new   RatedDVD(3, "Se7en", 7, 11.11, "R");
        RatedDVD d4 = new   RatedDVD(4, "Oceans Eleven", 11, 9.02, "PG13");
        RatedDVD d5 = new   RatedDVD(5, "Hitch Hikers Guide to the Galaxy", 42, 10.00, "G");    
        RatedDVD d6 = new   RatedDVD(6, "The Invisible Man", 0, 4999.59, "G");  

        // create inv and enter values
        inv = new Inventory();
        inv.add(d1);
        inv.add(d2);
        inv.add(d3);
        inv.add(d4);
        inv.add(d5);
        inv.add(d6);

        inv.sort();

        //make nButtons
        nButtonJPanel.setLayout( new GridLayout( 1, nNames.length, 5, 5 ) );
        nButtons=new JButton[nNames.length];
        for(int c=0;c<nButtons.length;c++){
            nButtons[c]=new JButton(nNames[c]);
            nButtons[c].addActionListener( this );
            nButtonJPanel.add(nButtons[c]); }
        add( nButtonJPanel, BorderLayout.NORTH );

        //make labels
        labelJPanel.setLayout( new GridLayout( labeled.length, 1 ));
        labels=new JLabel[labeled.length];  
        for(int c=0;c<labels.length;c++){
            labels[c]=new JLabel(labeled[c]);
            labels[c].addActionListener( this );
            labelJPanel.add(labels[c]);}
        add( labelJPanel, BorderLayout.WEST );

        //make fields
        fieldJPanel.setLayout( new GridLayout( fieldsBlank.length, 1 ));
        fields=new JTextField[fieldsBlank.length];
        for(int c=0;c<fields.length;c++){
            fields[c]=new JTextField(fieldsBlank[c],28);
            fields[c].addActionListener( this );
            fields[c].setEditable(false);
            fieldJPanel.add(fields[c]); }
            populate(currentDisplay);
        add( fieldJPanel, BorderLayout.EAST );

        //make sButtons
        sButtonJPanel.setLayout( new GridLayout( 1, sNames.length, 5, 5 ) );
        sButtons=new JButton[sNames.length];    
        Icon dvd50 = new ImageIcon( getClass().getResource( "dvd50.jpg" ) );
        for(int c=0;c<sButtons.length;c++){
            sButtons[c]=new JButton(sNames[c]);
            sButtons[c].addActionListener( this );
            sButtonJPanel.add(sButtons[c]);}
        sButtons[2].setIcon( dvd50 );       
        add( sButtonJPanel, BorderLayout.SOUTH );
    }// end method

    public void populate(int currentDisplay)
    {
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    fields[0].setText(String.valueOf(inv.get(currentDisplay).getItem()));
    fields[1].setText(inv.get(currentDisplay).getName());
    fields[2].setText(inv.get(currentDisplay).getRating());
    fields[3].setText(String.valueOf(inv.get(currentDisplay).getUnits()));
    fields[4].setText(String.valueOf(nf.format(inv.get(currentDisplay).getPrice())));
    fields[5].setText(String.valueOf(nf.format(inv.get(currentDisplay).value())));
    fields[6].setText(String.valueOf(nf.format(inv.get(currentDisplay).fee())));
    fields[7].setText(String.valueOf(nf.format(inv.get(currentDisplay).feeValue())));
    fields[8].setText(String.valueOf(nf.format(inv.value())));
    }

    public void actionPerformed(ActionEvent e) {


    }
Was it helpful?

Solution

You need to make your GUI class implement ActionListener by changing

public class GUI extends JFrame 

to

public class GUI extends JFrame implements ActionListener

OTHER TIPS

You need to just import the header file.....

import java.awt.event.ActionListener;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top