cancel
Showing results for 
Search instead for 
Did you mean: 

Need code example for setting a label text

Former Member
0 Kudos

Hello,

Lets say I have a label called testLabel and I would like to put it's text at the doInit() method of my view.

How do I do that?

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Also you got (technically) right answer, there is always preferred (as it emphasized by WD team on this forum) to use <b>context binding</b> over direct UI element properties manipulation.

In brief, if you declare MyAttr attribute in context, and bind it to label text, then you may just call wdContext.currentContextElement().setMyAttr("whatever-text-you-want");

So always consider context binding first.

VS

Former Member
0 Kudos

I fully agree with VS, try your best to access values thru' context binding and not dynamic programming. As we say WebDynpro is mostly a static approach for designing User Interfaces so we should maintain it.

Regards,

Shubhadip

Former Member
0 Kudos

Hi,

follow this link

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/documents/a1-8-4/web dynpro tutorial and sample applications.faq#q-1

<b>How to create your first Web Dynpro application?</b>

Above application is similar to your requirement but here textview you should same steps but your case is label all that you need to create a context attribute, label UI element map this attribute (in Properties of label UI element) and set the string (Code mentioned by Subdhip Gosh) in the doinit method or if you are declaring an Action ( in this action method depending your requirement)

Hope it helped you

Regards,

RK

Former Member
0 Kudos

Hi Roy,

do you want to set the label text dynamically from the wdDoInit method? If this is the case then just create a string type value attribute in view context (say ctxTestLabel. Create a label UI element (testLabel as u mentioned), bind the 'text' proerty of this label element to the context attribute ctxTestLabel.

In the wdDoInit() method of your view write the following line

wdContext.currentContextElement().setCtxTestLabel("Your label text");

This should solve the problem if I have judged it properly. If thats not the case you want to be let us know.

Regards,

Shubhadip

Former Member
0 Kudos

Hi Shubhadip,

What you have offered me is how to bind a text label to a context attribute. My question is different:

I have a label and I want to assign a text to it which will not be bound to an attribute from the Context Controller. Simply a "free" String.

How do I do that?

Message was edited by: Bertram Ganz

databinding: from UI-element property to context element not vice versa.

Former Member
0 Kudos

Hi Roy,

if you want to set the label directly without setting it thru context variable then its part of the dynamic programming of WD. So u can do that only in wdDoModifyView method. Obtain the reference of the label object from the view object and set the text property of it with the desired value.. like below

public static void wdDoModifyView(IPrivateImageAppView wdThis, IPrivateImageAppView.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime)

{

//@@begin wdDoModifyView

IWDLabel myLabel = (IWDLabel)view.getElement("testLabel");

myLabel.setText("Hello this is a Label");

//@@end

}

this should solve ur problem...

regards,

Shubhadip

Former Member
0 Kudos

10X! it indeed solved my problem.