Results 1 to 2 of 2
  1. #1
    fractal5 is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

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

    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);
    		  }
    		}
    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:
    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:

    Java Code:
    add(Set(1));
    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:

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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Using a function from another class

    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:

    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();
        }
    }
    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).

Similar Threads

  1. Replies: 3
    Last Post: 01-27-2012, 12:20 AM
  2. Adding to a function from a different class
    By JavaWizKid in forum New To Java
    Replies: 8
    Last Post: 11-15-2011, 12:19 AM
  3. Replies: 0
    Last Post: 10-23-2011, 11:49 PM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 08:35 PM
  5. Register a callback function in other class
    By barts2108 in forum New To Java
    Replies: 2
    Last Post: 11-10-2008, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •