Solved

ActionListener error

Posted on 2011-02-22
8
383 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
Comment Utility
       title = JOptionPane.showInputDialog("Enter Title");

title is not defined
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
your missing the import for ActionListener

import java.awt.event.ActionListener;
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:t38
Comment Utility
thanks a lot, i've a lot to learn..  
0
 

Author Comment

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

Author Comment

by:t38
Comment Utility
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
Comment Utility
>         //contentPane.add(button1);

you have commented out adding the button to your gui
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
nestparen challenge 4 55
json format text only 4 65
wordappend challenge 8 84
Python Assistance 7 31
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now