1,105,456 Community Members

how to get values from a jtext field in the main class from another class?

Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Pls can anyone help me with a code that will help me get values in a main class from another class...

I have two classes, one is user class that has the main method and the other which is process class.
I am having a jtextfield and a jbutton in the user's class, I want a method in the process class that can change the value of the jtextfield in the user class when the jbutton is clicked, it will call the the method in the process class that will do that...

Member Avatar
stultuske
Posting Expert
5,333 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

by adding a setter, I assume

public String getFieldValue(){
  return field.getText();
  }
Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Pls can you help me write the code that will be on the user class and the code that will be on the process class cos I have tried the above code and it didn't work...thanks in advance

Member Avatar
stultuske
Posting Expert
5,333 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

well, duh .. I don't know what your variables are called, so I can't use the correct name, now can I? neither do I know how your classes are related. I just assume your main class instantiates a JFrame of which you want to get the value of the field. but then, how do you think of triggering that? well, sure, tons of way to do that, anyway..

String theValue = instantiatedJFrame.getFieldValue();

and just like that, I've given you the code for both classes. how to implement it correctly, and how to use it correctly, that's up to you to decide/find out

Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry I have tried everything but it didn't work...

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

it didn't work...

Post the full text of the error message and the code that is causing the error.

Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok Let me just explain again, I'm trying to create a calculator, I have d jtextField that shows the answer and d variable name is ans,and another jbutton that will clear d jtextField if it is not empty and d variable name for the button is clear, so what am asking for now is instead of putting the code that will clear the JTextField in the JButton, I want the code to be in a method in class clear, so that from my main calculator class I can create an object of the clear class on the clear button to perform that action of clear...

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Why did the code you tried not work? Please explain.

The methods in one class can call the methods in another class if they have a reference to the other class object. For example:
AClass aClassRef =new AClass(); // one way to get a reference to an AClass object
aClassRef.someMethod(); // call someMethod in the AClass

Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

Ok the problem is from the clear class, just give me a code that the method of d clear class can access jtextfield variable named ans, which is in the main calculator class, am having problem getting dat ans variable in d clear class from the calculator class...

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Please post code for a small simple program that shows what you are trying to do.

What is a "d clear" class? d is a poor name for a class. Try to find a name that has some meaning when you see it used. Class names should start with an uppercase letter.

Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Sorry for using d, d is the short form of the.

Pls just visit this link to download the zip file containing the calculator class and the clear class

Link: http://www.maxinville.com/cals.zip

The fields you work with are:

  1. The JTextField with variable name ans, this is what displays the answer
  2. The JButton field with variable name clear and Text on it 'C'
    The object of the clearField method should be in the variable clear

The code to clear or set a value to the JTextField with variable name ans should be in the clear class and in the method clearField.

Pls when you are done this is my email to reach me email: kelvinrolex3(at)gmail.com

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

Sorry I don't care to visit other sites.
Please post code here for a small simple program that shows what you are trying to do.
Not the full project, just a small program for the specific problem.

Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok let me give a simple code

For the calculator class I have d following

A JTextField with variable name ans: this JTextField have some numbers or characters on it

Then A JButton with variable name clear, so I want when you click on this clear button it should delete every character or numbers from the JTextField with variable name ans.

The clear class that contains the code to do the delete should look like this

public class clear{
public void clearField() {
//the code that will delete d numbers from the JTextField with variable name ans in the calculator class

  }

}

All I just need is a method that can help me access ans variable in the calculator class from the clear class so that I can create an object of the that class in the clear button from the calculator class in the JButton with variable name clear...

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

delete d numbers from the JTextField with variable name ans in the calculator class

To do something like deleteing d numbers from a textfield, you should have a method in the Calculator class that you pass d and have it do the deletion.
Define the methodToRemoveNbrs() in the Calculator class.
aRefToCalcatorCls.methodToRemoveNbrs(d);

The Clear class needs to get the reference (aRefToCalcatorCls) to the Calculator class. Many ways to do this. A common one is to pass a reference to the Calculator class to the Clear class's construtor.

access ans variable

Define a method: getAns() in the Calculator class that returns the value of ans when called.

Member Avatar
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok thanks...

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: