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
Solved

ActionListener error

Posted on 2011-02-22
8
387 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

861 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