We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,280 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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...

3
Contributors
14
Replies
6 Hours
Discussion Span
1 Year Ago
Last Updated
15
Views
maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

by adding a setter, I assume

public String getFieldValue(){
  return field.getText();
  }
stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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

maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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

stultuske
Industrious Poster
4,379 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

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

maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

it didn't work...

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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...

maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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...

maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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...

maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Ok thanks...

maxinville
Newbie Poster
18 posts since Mar 2012
Reputation Points: 7
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1253 seconds using 2.89MB