Java Event Listener integer problem.

Discussion in 'Java' started by Mmm_moo_cows, Oct 24, 2004.

  1. Mmm_moo_cows

    Mmm_moo_cows Guest

    Hi,

    Pretty new to java and I have hit a small problem with event
    listeners, so any help would be very much appreciated.

    I wrote a simple program with a jframe and button. When the button is
    clicked, the event listener displays a simple confirmation message in
    the console via the System.out.print command.

    My problem is I have created an anonymouse inner class (I think its
    called that), which is giving me alot of strife when I want to use
    integers/objects from my main program.

    The simplest example I would like to program is a simple button click
    counter. i.e. every time you click a button a message could be
    displayed in the console - "Number of clicks = " + counter .

    This is what I have so far:-

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    class gui
    {

    public static void main (String[] args)
    {


    JFrame frame = new JFrame("Frame");
    frame.setSize(150, 70);

    Container content = frame.getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());

    JButton clickme = new JButton("Click Me!");
    content.add(clickme);

    frame.setVisible(true);


    clickme.addActionListener(new ActionListener()
    {

    public void actionPerformed(ActionEvent e)
    {
    System.out.println("You clicked me");
    }
    }
    );
    }

    }

    Every time I try and include a variable from the main gui class as a
    counter I get an error message:

    'local variable a is accessed from within inner class; needs to be
    declared final'

    The real use/application I am looking for is if I had some text boxes
    or combo boxes etc (i.e. user input) and a submit button or something.
    So, when you click the button it goes into the action event listener
    and pulls the input data from those input boxes on the first form, so
    it can work something out.
    An example may be having 2 text boxes for 2 numbers and a calculate
    button which will add them us and display the result in the system
    console. (keeping it simple for now!). I'm not too sure how I would
    get data from input boxes from within an inner class as well, i.e.
    like getting ahold of those two numbers whilst in the inner class from
    that example above.

    Any help would be much appreciated. I have been searching on the
    internet and can only find information about setting the event
    lisenter up to do an event, but nothing about actually using
    objects/variables in the event.

    Or am I approaching this a completely wrong way? if so could you
    please point me in the right direction.

    Thanks for any help in advance, it will be greatly appreciated.

    Cheers, Jon
    Mmm_moo_cows, Oct 24, 2004
    #1
    1. Advertising

  2. Mmm_moo_cows wrote:
    > 'local variable a is accessed from within inner class; needs to be
    > declared final'


    This is the way the language is designed. May I suggest you work through
    this part of Sun's Java tutorial to understand what is going on:

    http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
    http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html
    http://java.sun.com/docs/books/tutorial/java/javaOO/QandE/nested-questions.html

    If you are adventurous, you can also have a look at the Java language
    specification at Sun's web site.

    > Or am I approaching this a completely wrong way? if so could you
    > please point me in the right direction.


    It is difficult to say, because we don't know what else you want to do
    with your program. May I suggest you first sort out your problem with
    using inner classes, which is independent from GUI programming, and then
    you decide which of the possible ways you want to implement.

    For the future, please post beginner's questions to comp.lang.java.help,
    and advanced GUI questions to comp.lang.java.gui.

    /Thomas
    Thomas Weidenfeller, Oct 25, 2004
    #2
    1. Advertising

  3. Hi Cowboy,

    Mmm_moo_cows wrote:
    [...]
    > Every time I try and include a variable from the main gui class as a
    > counter I get an error message:
    >
    > 'local variable a is accessed from within inner class; needs to be
    > declared final'


    This message exactly describes your problem: you try to access a local
    variable from your inner class. Either you have to declare your variable
    final or you have to use an instance variable, e.g.

    class Gui {
    private int clicks = 0;

    public Gui() {
    }

    public JFrame createFrame() {
    JFrame frame = new JFrame("A new JFrame");
    frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
    JButton clickMe = new JButton("Click Me!");
    frame.getContentPane().add(clickMe);
    frame.pack();

    clickMe.addActionListener( new ActionListener() {
    public void actionPerformed( ActionEvent e ) {
    clicks++;
    System.out.println("Clicked " + clicks + " times");
    }
    } );

    return frame;
    }


    public static final void main( String args[] ) {
    Gui gui = new Gui();
    gui.createFrame().setVisible(true);
    gui.createFrame().setVisible(true); //2nd frame
    }
    }


    Bye
    Michael
    Michael Rauscher, Oct 25, 2004
    #3
  4. Mmm_moo_cows

    Mmm_moo_cows Guest

    Thanks for your help the links were useful, I did search the java
    tutorial but some of the ones you posted in my results.

    Cheers, Jon
    Mmm_moo_cows, Oct 26, 2004
    #4
  5. Mmm_moo_cows

    Mmm_moo_cows Guest

    Hi,

    Thanks very much for your help and the attached code. That and the
    some of the links provided in the other post (thanks) have really
    helped me piece this all together.

    Thanks again, Jon.
    Mmm_moo_cows, Oct 26, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sasha Borodin
    Replies:
    0
    Views:
    3,078
    Sasha Borodin
    Jul 28, 2003
  2. Slobodan C

    Event listener list implementation

    Slobodan C, Jan 22, 2004, in forum: Java
    Replies:
    2
    Views:
    1,173
    Slobodan C
    Jan 23, 2004
  3. Mike Scirocco
    Replies:
    4
    Views:
    257
    Mike Scirocco
    Apr 5, 2007
  4. carlos
    Replies:
    1
    Views:
    143
    Paul Wilkins
    Feb 16, 2008
  5. barry_normal

    Event listener problem

    barry_normal, Feb 24, 2011, in forum: Javascript
    Replies:
    7
    Views:
    129
    barry_normal
    Feb 24, 2011
Loading...

Share This Page