This won't even compile, are you sure it's the code you are running?
If you are getting an Exception then please include the full message and stack trace in your post.
Why are you creating a new DisplayWindow every time the button is pressed. The fields in the newly created class won't reference the components on the screen.
Good Practice Note: Don't use the same action listener for more than one component unless those components perform the same action. If you ever find yourself writing code like "if(ae.getSource()==guiModel._button)" in your action listener then your design is probably wrong.
The code is compiling and running. The reason I included the 'ae.getSource()==guiModel._button' was to get the button click. I basically want to get the JTextField's value to the other class. So what should I be doing here?
Joined: Aug 07, 2007
The code is compiling and running.
Your DisplayWindow class doesn't have a field named _textField, it does however have a field named textField so unless you just haven't shown the declaration of _textField the code you posted won't compile.
The reason I included the 'ae.getSource()==guiModel._button' was to get the button click.
Being able to explain why you did it doesn't make it good design. The fact that you have to test if the event source was a particular component shows the design is wrong.
I basically want to get the JTextField's value to the other class
There are several ways of doing this, such as:
You could pass a reference to the JTextField to the ActionListener either when you create it or via a method call.
You could pass a reference to the DisplayWindow object to the ActionListener and have DisplayWindow implement a method such as getMyTextFieldValue() which returns the value.
You could declare the ActionListeningModule as an inner class of the DisplayWindow class in which case it would be able to directly access the textField filed.
Hi just wanted to update you that I got it working. The DisplayWindow's object was avaliable in the main class. So I extended the main class to the class I needed the value. Also wrote a return function in the main class which gets me the value I wanted from the textField.
@Campbell: I also made changes to the names took out the underscores.
Thanks a lot for the help guys.
Joined: Aug 07, 2007
Glad to hear you got it working but that sounds like a dreadful solution from a design point of view.
Paul Clapham wrote:There's no point in extending URLValidator. It only has static members. So I would leave that off.
I agree that ActionListeningModule shouldn't extend URLValidator but also URLValidator shouldn't have a static getVal() method.
This method is reliant on main() being called first and is just a hack to get around the OP's design problem.
I suggested 3 possible solutions in an earlier post, if you don't understand what I meant then please ask for clarification and I will do my best to explain it better.
Also why are you still adding the listener to both the text field and the button?
As per Paul's advise I am not extending the URL Validator.
At first wrote the getVal() method in the DisplayWindow class, but when I try to get the textfield value to the ButtonActionListener class I am getting a null value, that is the reason why I wrote the getVal method in the URLValidator class. If you could give me some clarification on how to get the textField value from the DisplayWindow class that would be great.
PS: The ActionListener for the textfield was added by accident. Forgot to remove it.
Joined: Aug 07, 2007
By far the easiest solution is to use an inner class. The most common way of writing an ActionListener is to use either an anonymous inner class or an inner class. Generally if the action code will only be called by one component then an anonymous inner class is used.
Having said that, as you already have your action listener code in a class the easiest thing to do is to make it into an inner class of DisplayWindow ie move all the code (except import statements) to just before the final closing brace of the DisplayWindow class and add any missing import statements to the top of the DisplayWindow class. As an inner class ActionListeningModule will have access to textField and so you can directly call getText() on it.
BTW all the instance variables in DisplayWndow should be private.