im trying to add a button to my program that will clear the text from the text area but i cannot get rid of one error. the program is made up of anumber of panels. the part of the code concerned here is as follows:
That's because the listener does not have a reference to the TextField.
You need some reference to something that can then get access to the TextField.
So you can do this many ways.
In your Listener class you only have the ActionEvent which will only return the Button. But you should be able to get a hold of its container the JPanel Holder. Change the name to lowercase to meet the Coding Standard set by Java.
So the question then becomes, can you get to the TextField from there. Does JPanel have a method to get all the Components that are in the Panel. And then loop through those till you find the TextField.
Personally, that is the long way around. and with OO techniques, it would be easier, but that would take a long time to describe. But basically you would have more classes, like the JPanel Holder being a class with a method called setMessage() and that class has an instance variable holding the JTextfield, that that method setMessage() can simple call the TextFields setText() method.
thanks mark, i wasnt completely sure of what you meant but i thought that it may be because "message" is the name of the panel that there was an error. as a result i changed "message" to just a text area but the same error exists:
It is still a reference issue. Basically in your ActionListener code you do not have access to the reference variable, you need to be able to reference something to call a method on that reference. A JPanel does not have a setText() method in its class.
Mark
Chris Lavery
Ranch Hand
Joined: Nov 30, 2004
Posts: 54
posted
0
thanks mark, iv got it now. i was creating the textarea in my buildGUI method so i took it out and put it before the method as a field and it works fine now. Thanks for your help Chris