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

Recommended Answers

All 14 Replies

by adding a setter, I assume

public String getFieldValue(){
  return field.getText();

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

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

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

it didn't work...

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

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

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

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

commented: Just demand code. Does not respond to help or questions. -3

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.

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


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)

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.

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

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.

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.

Ok thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.