Solved

ActionListener error

Posted on 2011-02-22
8
388 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 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