Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 3
Registered: ‎02-20-2012
My Device: Bold 9700
My Carrier: Koodo
Accepted Solution

Using a function from another class

[ Edited ]

Information: Blackberry JDE 7.1.0

BlackBerry 9930 Simulator - V 4.0.0.141

 

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.

 

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.

 

	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:

 

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:

 

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.

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Using a function from another class

Try this:

 

HorizontalFieldManager hr = Set(1);
add(hr);
...
...
Field[] fields = hr.getFields();
String val = fields[0].getText();

 

Another option is to extend an HorizontalFieldManager and to keep a reference to the new fields.

 

class MyHorizontalFieldManager extends HorizontalFieldManager 
{
private TextBoxField set1W = new TextBoxField(40,25);
private TextBoxField set1R = new TextBoxField(40,25);

public MyHorizontalFieldManager(...)
{
super(...);
...
}

public TextBoxField getSet1W()
{
return set1W;
}

public TextBoxField getSet1R()
{
return set1R;
}

}

 

And then use the MyHorizontalFieldManager in the 'Set' function.

 

Highlighted
New Contributor
Posts: 3
Registered: ‎02-20-2012
My Device: Bold 9700
My Carrier: Koodo

Re: Using a function from another class

Thanks for the suggestion. I figured out another workaround. Basically implementing an array of TextBoxFields, and then passing them one by one the to the method 'Set()'.

 

I had tried this initially before posting here but due to a careless error when declaring the array of TextBoxFields, I couldn't get it to work.

 

Anyway the topic can be closed now, as the problem has been solved. Thank you.