Using a function from another class
The topic title sounds simple, but I'm actually trying to use a function X from class A, inside a function Y in class B, and the get the value returned by X to a variable in B.
Let me start by explaining what I currently have and then go onto what I'm trying to get to.
I am using this class, the code for which I found online, to create a scrollable multiline textbox.
Code:
public class TextBoxField extends VerticalFieldManager {
//define some variables to be used
//in the class
private int managerWidth;
private int managerHeight;
private EditField editField;
public TextBoxField(int width, int height) {
super(Manager.NO_VERTICAL_SCROLL);
managerWidth = width;
managerHeight = height;
VerticalFieldManager vfm =
new VerticalFieldManager
(Manager.VERTICAL_SCROLL);
editField = new EditField(){
public void paint(Graphics g) {
getManager().invalidate();
super.paint(g);
}
};
vfm.add(editField);
add(vfm);
}
public void paint(Graphics g) {
super.paint(g);
g.drawRect(0, 0, getWidth(), getHeight());
}
public void sublayout(int width, int height) {
if (managerWidth == 0) {
managerWidth = width;
}
if (managerHeight == 0) {
managerHeight = height;
}
super.sublayout(managerWidth, managerHeight);
setExtent(managerWidth,managerHeight);
}
public String getText() {
return editField.getText();
}
public void setText(String text) {
editField.setText(text);
}
}
Then in another class I've created a function 'Set' which instantiates the textbox class, to create two textboxes and a richfieldtext, add them to a HorizontalFieldManager, and return the HorizontalFieldManager.
Code:
HorizontalFieldManager Set(int set){
HorizontalFieldManager hr1=new HorizontalFieldManager(FIELD_HCENTER);
RichTextField set1 = new RichTextField("SET " + set + ":"){
protected void layout(int width, int height)
{
super.layout(75,30);//Width and height according to the button name;
setExtent(75,30);//width and height according to the button name;
}
};
set1.setPadding(10, 5, 10, 10);
TextBoxField set1W = new TextBoxField(40,25);
set1W.setPadding(10, 5, 10, 0);
TextBoxField set1R = new TextBoxField(40,25);
set1R.setPadding(10, 5, 10, 0);
hr1.add(set1);
hr1.add(set1W);
hr1.add(set1R);
return hr1;
}
Now in my main class I'm calling this function to add a 'Set' to the screen using:
The reason for creating the 'Set' function instead of using the instance of the textbox class in the main class itself, is because I will be creating several 'Sets'
Now as you can see there is a function public String getText() in the TextBoxField() which returns the value in the editField. What I need to do is get the value in this field once a button is pressed.
I have the code for the button function so that's not a concern. However I don't know how to retreive the editField value when the button is pressed. I can get the value inside the function Set(), using:
Code:
String getval = set1W.getText();
but can't reach it from outside. I don't even know if it is possible in the first. If it isn't could someone suggest a way where I could retreive these values.
Any help is appreciated.
Thanks.
Re: Using a function from another class
Quote:
I'm actually trying to use a function X from class A, inside a function Y in class B, and the get the value returned by X to a variable in B.
That much is straight forward:
Code:
class A {
int x() {
return 42;
}
}
class B1 {
private int var;
void y(A a) {
var = a.x();
System.out.println("Variable var in B has been set to " + var);
System.out.println("(the value returned by x() in class A)");
}
}
class B2 {
private int var;
private A a;
public B2(A a) {
this.a = a;
}
void y() {
var = a.x();
System.out.println("Variable var in B has been set to " + var);
System.out.println("(the value returned by x() in class A)");
}
}
public class Test {
public static void main(String[] args) {
new B1().y(new A());
new B2(new A()).y();
}
}
Note that for method y() of class B to call method x() of class A y() must have access to some instance of A. The code illustrates this instance being passed either directly to y() or to B's constructor. (A setter method in B would be another way.)
-----
After that your post loses me. Perhaps you could post a SSCCE? Something brief (ie without the layout or other stuff unrelated to the problem) and able to be compiled (ie without the missing dependencies and other code).