Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ActionListener error

Posted on 2011-02-22
8
Medium Priority
?
393 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Accepted Solution

by:
objects earned 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

610 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