• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in getting JTextField value to another class

 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am having a silly problem, I have 2 different class am I trying to get the value of a JTextField class from one class to another, could someone please help me out...



The second part is as follows



I am getting a null pointer exception on button click, could someone please help with this.

Thanks.
Vineeth
 
Tony Docherty
Bartender
Posts: 2991
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,

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?
 
Tony Docherty
Bartender
Posts: 2991
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please change probably wrong to wrong in Tony Docherty’s post. There is some poor style, with all those names beginning with _ and other poorly‑chosen names.

I shall move this post to the GUIs forum, where it would fit better.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,

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.
 
Tony Docherty
Bartender
Posts: 2991
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad to hear you got it working but that sounds like a dreadful solution from a design point of view.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well this is the completed code...if you do have some suggestions that would be great.







Please do let me know if any improvements can be made.

Thanks
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why I wrote it like this is because I wanted the GUI and the logic to be separated. In the beginning I had written the action listener in the DisplayWindow class, but then changed it.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


There's no point in extending URLValidator. It only has static members. So I would leave that off.
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Paul.... cheers
 
Tony Docherty
Bartender
Posts: 2991
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

@vineeth
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?
 
Vineeth Menon
Ranch Hand
Posts: 79
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,

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.
 
Tony Docherty
Bartender
Posts: 2991
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic