Results 1 to 2 of 2
- 02-23-2012, 03:02 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
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.
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.Java 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); } }
Now in my main class I'm calling this function to add a 'Set' to the screen using:Java 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; }
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'Java Code:add(Set(1));
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:
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.Java Code:String getval = set1W.getText();
Any help is appreciated.
Thanks.
- 02-23-2012, 07:28 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Using a function from another class
That much is straight forward: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.
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.)Java 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(); } }
-----
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).
Similar Threads
-
calling a function of a specific type in a java class from another class.....
By safi532 in forum Advanced JavaReplies: 3Last Post: 01-26-2012, 11:20 PM -
Adding to a function from a different class
By JavaWizKid in forum New To JavaReplies: 8Last Post: 11-14-2011, 11:19 PM -
Can't Get variable calculated in another class to function in main class
By hkp30 in forum New To JavaReplies: 0Last Post: 10-23-2011, 10:49 PM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Register a callback function in other class
By barts2108 in forum New To JavaReplies: 2Last Post: 11-10-2008, 04:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks