Solved

ActionListener error

Posted on 2011-02-22
8
390 Views
Last Modified: 2012-05-11
ok, if you're a java programmer you might want to avert your eyes, the code here is a mess but i'm trying to work through a problem and i can't see why the 'actionlistener' is giving an error i.e. eclipse is underlining it in red and giving an error message,  any suggestions please

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class BookGUI extends JFrame implements ActionListener

{

      String title  = "";
      
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;

    //Creates & displays a window of the class FlowLayoutDemo
    public static void main(String[] args)
    {
        BookGUI gui = new BookGUI( );
        gui.setVisible(true);
    }
 
       
    public BookGUI( )
    {
        setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer( ));
        setTitle("GUI Assignment");
        Container content = getContentPane( );

        content.setLayout(new FlowLayout());
        //JButton yellowButton = new JButton("Yellow");
        //yellowButton.addActionListener(this);
        //contentPane.add(yellowButton);

        //else if (e.getActionCommand().equals("Yellow"))
        //contentPane.setBackground(Color.yellow);
       
        JButton button1 = new JButton("Title");
        content.add(button1);
        button1.addActionListener(this);
        //contentPane.add(button1);

    }
    public void actionPerformed(ActionEvent e)
    {
       
        if (e.getActionCommand().equals("Title"))
        title = JOptionPane.showInputDialog("Enter Title");
       
       
       
        JButton label2 = new JButton("Get Book Information");
        content.add(label2);

        JButton label3 = new JButton("Show Total Cost of Books");
        content.add(label3);
   
    }
}


0
Comment
Question by:t38
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 34956144
       title = JOptionPane.showInputDialog("Enter Title");

title is not defined
0
 
LVL 92

Expert Comment

by:objects
ID: 34956156
scrastch that, didn't see it

you need to define content


        Container content = getContentPane( );

        JButton label2 = new JButton("Get Book Information");
        content.add(label2);

        JButton label3 = new JButton("Show Total Cost of Books");
        content.add(label3);
0
 

Author Comment

by:t38
ID: 34956279
Thanks for the quick response, much appreciated, could you look at the attached screenshot please?  for some reason that i can't fathom eclipse is showing an error in the actionlistener but the screenshots should illustrate the problem..

 error1 error2
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34956401
your missing the import for ActionListener

import java.awt.event.ActionListener;
0
 

Author Comment

by:t38
ID: 34956452
thanks a lot, i've a lot to learn..  
0
 

Author Comment

by:t38
ID: 34956459
one final question, when i run this it displays as a blank frame, not buttons, any suggestions please?
0
 

Author Comment

by:t38
ID: 34956471
that's ok, i got that one, i had commented out the content pane section, thanks for all your help.
0
 
LVL 92

Expert Comment

by:objects
ID: 34956474
>         //contentPane.add(button1);

you have commented out adding the button to your gui
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question