Codename One Get component reference in a List - java

Well, I have another doubt. Every row of my list have those components
Label - Button(-) - Button(+) - Label(0) when I clicked on Button(+) I need to get Label(0) value and increase one unit. So I need to get Label(0) reference to set new values. I am trying to find this component with:
Label l = (Label)findByName("lblVal", c.getParent());
Label l = findLblVal();
Label l = findLblVal(c);
Label l = findLblVal(c.getPrent());
My code in List Action Listener is:
List list = (List)c;
Button b = ((GenericListCellRenderer)list.getRenderer()).extractLastClickedComponent();
if(b != null)
{
//lblVal is the name of my component in the renderer
//MY PROBLEM IS HERE, I GET NULL REFERENCE
Label l = findLblVal(c);
}
but I always get a null reference. How can I get reference to this component?

Renderer's are stateless. I suggest reading the developer guide or following the How Do I? videos.
You need to edit your model so that it includes a field matching the name of the label with the proper string that you want.

Related

Is it possible to get a component from layout in Vaadin by labeling it?

I have a question. Is it possible to get a component from a layout in Vaadin by labeling it with a specific name or something?
Asume that we have this code.
NumberField totalSamples = new NumberField();
totalSamples.setValue(0d);
totalSamples.setEnabled(false);
Label label = new Label("Total samples:");
Button start = new Button("Start");
row = new HorizontalLayout();
row.add(start, label, totalSamples);
layout.add(row);
If I want to get the label object from layout and first need to get the row object and if I don't know the index of all of them. Is there some way to get these objects by setting a specific number or name to them?
Button start = layout.getComponentAt(index)
Use a collection.
Vaadin objects are simply Java objects. So, as you instantiate the widgets, add them to a List or other collection that fits your needs.
Your collection can be stored as a member variable on your outer layout class.
In your specific case of needing to track dynamically-created rows where each has three widgets across, create a class. That class should extend HorizontalLayout. The class would have three member variables, each named so you can later access them individually.
class Row extends HorizontalLayout {
NumberField totalSamples ;
Label label ;
Button start ;
// Constructor
public Row ( … ) {
totalSamples = … ;
label = … ;
start = … ;
this.add( label , totalSamples , start ) ;
}
}
You could add other member variables to this Row class besides Vaadin widgets. If each row represents a particular product made by your company, the add a productId field. Or perhaps you you want to track each row individually for logging, debugging, or audit trail. If so, add a UUID field to the class.
That Row class could be nested within the outer layout class, as you’ll not need it anywhere else in your app.
On your outer layout, instantiate, collect, and place the rows.

Store instances into an array in java

I am working on a project where I have a bunch of buttons, mostly split into two groups and I would like to work with these groups through an array. Each button is an instance of class Button extends JButton and each instance has its own value (this.value = "..")
The problem is that it seems like array is being filled with previously mentioned instances, but when I try to reach them, it acts like array is filled with nulls.
Button but1, but2, but3;
Button[] buttonNumbers = {but1, but2, but3};
System.out.println(buttonNumbers.length); // returns 3, so it acts like array IS filled
System.out.println(but1.value); // prints whatever the value is
System.out.println(buttonNumbers[0].value); // throws error, element acts like null
Could someone help me out, where is the problem or what am I missing ?
Thank you for every tip or answer!
It doesn't seem like you're initializing each Button! You need to call its constructor for each Button with whatever parameters you defined:
Button but1 = new Button(), but2 = new Button(), but3 = new Button();
Button[] buttonNumbers = {but1, but2, but3};
System.out.println(buttonNumbers.length); // returns 3, so it acts like array IS filled
System.out.println(but1.value); // prints whatever the value is
System.out.println(buttonNumbers[0].value);

Jira - Post-function to clone an issue multiple times based on issue's custom field value

An issue has a multi-select custom field (checkboxes) with at least one selected option.
I'm trying to create a script for a workflow post-function that should do the following.
The easy part:
If the custom field has only one option selected, simply set issue type depending on the selected option and assign the issue to current user.
The tricky part (the one I could use some help with):
If the custom field has more than one option selected, then the issue's custom field should be updated to the first selected option, and cloned as many times as the number of remaining selected options. After that in each created clone the custom field should be set to only one of the remaining options respectively and clone's issue type updated according to that selection.
For example, the issue has the custom field with options 'a', 'b' and 'c' selected. After the post function is triggered, it should create two clones, each of which will have that custom field set to 'b and 'c' respectively. The original issue will have its custom field value set to 'a'. And all respective issues should have their issue types set according to custom field option selected. On paper it should look something like this:
Before cloning:
original issue (cf = a,b,c; issue type = whatever)
After cloning:
original issue (cf = a; issue type = 'Type A')
first clone (cf = b; issue type = 'Type B')
second clone (cf = c; issue type = 'Type C')
and so on
The code:
This is the code I came up with:
import com.atlassian.jira.component.ComponentAccessor
def currentUser =
ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(13801)
def fieldMgr = ComponentAccessor.getFieldManager()
def optsMgr = ComponentAccessor.getOptionsManager()
def issueSvc = ComponentAccessor.getIssueService()
def issueTypes =
fieldMgr.getIssueTypeField().getOptionsForIssue(issue).getAt("id")
def svcList = optsMgr.getOptions(cf.getRelevantConfig(issue))
def svcIssueTypeMap = new LinkedHashMap()
def issueCf = new ArrayList()
for (i = 0; i < issue.getCustomFieldValue(cf).size(); i++){
issueCf.add(issue.getCustomFieldValue(cf)[i])
}
for (int i = 0; i < svcList.size(); i++){
svcIssueTypeMap.put(svcList[i],issueTypes[i])
}
if (issue.getCustomFieldValue(cf).size() > 1){
for (int i = 1; i < issueCf.size(); i++){
def cfValue = new ArrayList()
cfValue.add(issueCf[i])
issue.setCustomFieldValue(cf, cfValue)
issue.setIssueTypeId(svcIssueTypeMap.get(issueCf[i]))
def issueCfMap = new HashMap()
issueCfMap.put(cf,Optional.of(true))
def cloneVld = issueSvc.validateClone(currentUser, issue, issue.getSummary(), true, false, false, issueCfMap)
issueSvc.clone(currentUser, cloneVld)
}
}
def finalCfValue = new ArrayList()
finalCfValue.add(issueCf[0])
issue.setCustomFieldValue(cf, finalCfValue)
issue.setIssueTypeId(svcIssueTypeMap.get(issueCf[0]))
issue.setAssignee(currentUser)
As you can see, my idea is to set the values of the custom field and issue type in the current issue to the values that should be in the clone, then clone the issue, and repeat the process using the values for the next clone and so on.
After all clones have been created I finally set the original issue's custom field and issue types the to values they should have.
Everything works just fine as long as the custom field has only one option selected (the easy part). When the custom field has multiple options selected this is what happens:
Before cloning:
original issue (cf = a,b,c; issue type = whatever)
After cloning:
original issue (cf = a; issue type = 'Type A') (good!)
first clone (cf = a,b,c; issue type = 'Type A')
second clone (cf = a,b,c; issue type = 'Type A')
Please advise, what am I doing wrong here?
Cheers
The only way to change issue type is move issue.
I would recommend you to create new issues of type you need instead clone.

Using LiveCycle How can I create and populate a new row in a table with values from other fields with a button press?

Here is my current code, so far the button will create a new row in the table and it will reset the fields (AdditionalComments, CellLocation, and FailureSelection, which are all text fields), but it will not allow me to populate the original FailureRow or the new instance of FailureRow that is created:
field name="AddFailureButton" w="31.114mm" h="6mm">
event name="event__click" activity="click"
script contentType="application/x-javascript"
xfa.host.messageBox("Failure Recorded, Please Continue Filling Out the Form Until All Failures Have Been Recorded. Then Please Save and Submit the form.", "Continue/Save/Submit", 3);
this.resolveNode('failuretable.Table1._FailureRow').addInstance(1);
if (xfa.host.version < 8) {
xfa.form.recalculate(1);
}
//****need to set values of current FailureRow to be equal to CellLocation, FailureSelection, and AdditionalComments, THEN clear the values as written below*****
xfa.host.resetData("form1.page1.AdditionalComments");
xfa.host.resetData("form1.page1.CellLocation");
xfa.host.resetData("form1.page1.FailureSelection");
</script>
Try the following code:
var newRow = this.resolveNode('failuretable.Table1._FailureRow').addInstance(1);
newRow.CELL_NAME.rawValue = form1.page1.AdditionalComments.rawValue;
newRow.CELL_NAME.rawValue = form1.page1.CellLocation.rawValue;
newRow.CELL_NAME.rawValue = form1.page1.FailureSelection.rawValue;
Remember to replace CELL_NAME with a proper name.
btw. When you are referencing object in the script you can just use it's SOM expression failuretable.Table1._FailureRow instead of this.resolveNode('failuretable.Table1._FailureRow').

Getting content from JTextFields created dynamically

I am learning Java with Swing and I have some problems with using JTextField. In my program I want to dynamically add a few JTextFields with some text:
while( (ln = bufFile.readLine()) != null ) {
// inIdPanel is JPanel
inIdPanel.add(new JTextField(ln));
}
And it works good. However, the content of these JTextFields can be modified by users, and later I want to call getText() from all of them. Is this possible? How can I do this?
I saw this question: Java Swing: JButton creates new JTextField(s) but this isn't enough to solve my problem (I think using arrays in my case is not a good idea but maybe I'm wrong).
The reason why you cannot call getText() is that you have not stored a reference to the JTextField when you created it. You will need to use an array or collection to store the JtextFields as you create them so you can call the method on them later. A collection will be easier than an array because you do not know how many lines you will read in so you want it to be able to grow.
List<JTextField> fields = new ArrayList<JTTextField>();
while( (ln = bufFile.readLine()) != null ) {
JTextField field = new JTextField(ln);
inIdPanel.add(field);
fields.add(field);
}
Then you can call the .getText() from all of them
for(JTextField field: fields){
System.out.println(field.getText());
}
For an easy solution, just add an ArrayList<JTextField> textFieldList and add to the code you posted:
while((ln = bufFile.readLine()) != null) {
textFieldList.add(new JTextField(ln));
inIdPanel.add(textFieldList.get(textFieldList.size()-1));
}
Then, when you want to access the text fields, you simply iterate through them, e.g.
for (JTextField jtf : textFieldList) {
/* Operate on jtf, call methods, etc */
}
You could replace the ArrayList with an array if there is a defined limit on how many text fields you could add, but the list is nice if that quantity is unknown.

Categories