Java Event Listener integer problem.

M

Mmm_moo_cows

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
 
T

Thomas Weidenfeller

Mmm_moo_cows said:
'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
 
M

Michael Rauscher

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
 
M

Mmm_moo_cows

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
 
M

Mmm_moo_cows

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top