Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,413 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,602 people online right now. Registration is fast and FREE... Join Now!




ActionListeners / Swing

 

ActionListeners / Swing

Flipside

3 Jul, 2009 - 12:35 PM
Post #1

New D.I.C Head
*

Joined: 3 Jul, 2009
Posts: 8


My Contributions
I having trouble setting up my Action Listeners. This first class, MyFrame, is used to build the components of my UI. The second class, MyMenuBar, is one of those components. Now, I'm trying to set up Action Listeners for the Menu buttons in MyMenuBar and have MyFrame handle the Action Events.

When I compile, I get errors for the two lines in MyMenuBar:

item.addActionListener(this);

The errors say "addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (com.jne.MyMenuBar)"

I need help fixing this problem, I'm not really sure what's wrong. Please note, I've only posted two classes from my program. I can post the rest if needed.



MyFrame.java
CODE
package com.jne;

import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;

import java.awt.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;



public class MyFrame extends JFrame implements ActionListener {

    MyPanel myPanel;
    MyMenuBar myMenuBar;
    MyButtons myButtons;

    public MyFrame() {

        myPanel = new MyPanel();
        Container contentPane = this.getContentPane();
        contentPane.add(myPanel);

        myMenuBar = new MyMenuBar();
        this.add(myMenuBar, BorderLayout.PAGE_START);

        myButtons = new MyButtons();
        this.add(myButtons, BorderLayout.LINE_START);

    }

    public void actionPerformed(ActionEvent e) {

        //JFileChooser fileChooser = new JFileChooser();

        if(e.getActionCommand().equals("Open")) {
            Toolkit.getDefaultToolkit().beep();
        }
        else if(e.getActionCommand().equals("Save")) {
            Toolkit.getDefaultToolkit().beep();
        }
        else if(e.getActionCommand().equals("Exit")) {
            System.exit(0);
        }
        else if(e.getActionCommand().equals("Set Width")) {
            Toolkit.getDefaultToolkit().beep();
        }
        else if(e.getActionCommand().equals("Set Indent")) {
            Toolkit.getDefaultToolkit().beep();
        }
        else if(e.getActionCommand().equals("Set Numbering")) {
            Toolkit.getDefaultToolkit().beep();
        }

    }

}


MyMenuBar.java
CODE
package com.jne;

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

import java.awt.event.ActionListener;


public class MyMenuBar extends JMenuBar {

    MyPanel myPanel = new MyPanel();

    String[ ] fileItems = new String[ ] {"Open", "Save", "Exit"};
    String[ ] settingsItems = new String[ ] {"Set Width", "Set Indent", "Set Numbering"};

    public MyMenuBar() {

        JMenu fileMenu = new JMenu("File");
        JMenu settingsMenu = new JMenu("Settings");

        for(int i = 0; i < fileItems.length; i++) {
            JMenuItem item = new JMenuItem(fileItems[i]);
            item.addActionListener(this);
            fileMenu.add(item);
        }
        
        for(int i = 0; i < fileItems.length; i++) {
            JMenuItem item = new JMenuItem(settingsItems[i]);
            item.addActionListener(this);
            settingsMenu.add(item);
        }

        this.add(fileMenu);
        this.add(settingsMenu);
    }

}



User is offlineProfile CardPM
+Quote Post


pbl

RE: ActionListeners / Swing

3 Jul, 2009 - 12:47 PM
Post #2

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
Yes because your MyMenuBar does not implements ActionListener
but your MyFrame is an ActionListener

so

CODE


     myMenuBar = new MyMenuBar(this);     // pass my MyFrame which is an ActionListener to MyMenuBar

....

class MyMenuBar {

     // constructor
     MyMenuBar(ActionListener listener) {
....
     item.addActionListener(listener);


User is offlineProfile CardPM
+Quote Post

Flipside

RE: ActionListeners / Swing

3 Jul, 2009 - 12:50 PM
Post #3

New D.I.C Head
*

Joined: 3 Jul, 2009
Posts: 8


My Contributions
QUOTE(pbl @ 3 Jul, 2009 - 12:47 PM) *

Yes because your MyMenuBar does not implements ActionListener
but your MyFrame is an ActionListener


Perfect! It works. Thanks a lot.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 11:17PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month