Results 1 to 4 of 4
  1. #1
    Cymro is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default [SOLVED] Actionevent problem

    I have decided to shy away from IDEs to try my hand at a text editor (proper? :p) approach to coding. However, a major problem:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class SandBox extends JFrame{
    
    private static void makeGUI(){
    	JFrame frame = new JFrame("Test");
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLayout(new FlowLayout());
    	
    	JButton button = new JButton("Login");
    	button.addActionListener(new ActionListener());
    	frame.add(button);
    	
    	frame.pack();
    	frame.setVisible(true);
    
    }
    
    	public void actionPerformed(ActionEvent e){
    		System.out.println("Test");
    	}
    
    
    
    public static void main(String[] args){
    	SwingUtilities.invokeLater(new Runnable(){
    		public void run(){
    			makeGUI();
    		}
    		});
    	}
    
    }
    when this code compiles, it informs me that it “cannot find symbol” whenver ActionEvent/Listener is referred to. Not exactly what was meant to happen :(

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    C:\jexp>javac sandboxrx.java
    sandboxrx.java:21: cannot find symbol
    symbol  : class ActionEvent
    location: class SandBoxRx
        public void actionPerformed(ActionEvent e){
                                    ^
    sandboxrx.java:14: cannot find symbol
    symbol  : class ActionListener
    location: class SandBoxRx
            button.addActionListener(new ActionListener());
                                         ^
    2 errors
    1 - import java.awt.event.*;
    recompile:
    Java Code:
    C:\jexp>javac sandboxrx.java
    sandboxrx.java:15: java.awt.event.ActionListener is abstract; cannot be instantiated
            button.addActionListener(new ActionListener());
                                     ^
    1 error
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SandBoxRx implements ActionListener {
        private void makeGUI(){
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());
    
            JButton button = new JButton("Login");
            // The jvm is looking for the "ActionListener" class
            // on your classpath. Since ActionListener is a Java
            // class then this is doubly confusing.
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("anonymous ActionListener");
                }
            });
            button.addActionListener(this);
            button.addActionListener(new SandBoxListener());
            frame.add(button);
    
            frame.pack();
            frame.setVisible(true);
        }
    
        // Implementation of ActionListener interface
        // implemented by the enclosing class.
        public void actionPerformed(ActionEvent e){
            System.out.println("Enclosing class-implemented ActionListener");
        }
    
        private class SandBoxListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                System.out.println("SandBoxListener ActionListener");
            }
        }
    
        public static void main(String[] args){
            EventQueue.invokeLater(new Runnable(){
                public void run(){
                    new SandBoxRx().makeGUI();
               }
            });
        }
    }

  3. #3
    Cymro is offline Member
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Default

    Thank you very much. It all works now :)

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    I applaud your goal of working with your text editor instead of an IDE. It's my belief it will make you a better programmer- once you've got the fundamentals solidly down, you should move back to the IDE or only move back to the IDE when time constraints and many lines of code are required. They help to reduce production times.

    Now that your problem is sufficiently solved, take the time to mark your thread as "Solved" using the Thread Tools at the top.

    Best of luck. :)
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. [SOLVED] Problem with setVisible(); on LINUX
    By lepetitprince in forum AWT / Swing
    Replies: 9
    Last Post: 11-22-2008, 05:14 PM
  2. [SOLVED] alignment problem
    By nanimtech in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-10-2008, 02:23 PM
  3. [SOLVED] file i/o problem
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-06-2008, 07:42 PM
  4. [SOLVED] Problem with code - inheritence
    By yalla in forum New To Java
    Replies: 1
    Last Post: 03-30-2008, 07:11 AM
  5. ActionEvent example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •