2

Is it possible to dynamically create objects or modify them on run-time ?for example,on button click,another button created or change number of lines of a road? When I write this code for a button Action,in run-time

road123.setBackwardLanesCount(3);

I get error below:

root: road123: Markup element is already initiated and cannot be modified.Please use constructor without arguments,perform setup and finally call initialize() .function

M.ah
  • 147
  • 11
  • could you provide a bit more info about your code? what obj class is road123? how are you trying to add/remove the button/lines – Nikolaj Klitlund Børty Sep 05 '16 at 12:00
  • road123 is a Road object. – M.ah Sep 05 '16 at 20:01
  • well it is difficult to create object in code, and then add the dynamically to the GUI. You have to add it to the anylogic runtime engine. easiest way is to create the objects you need and then hide the ones you don't need, with button.setvisible(false). If however that is not and option. My best advice is to create a botton, and then look in the anylogic code for the main to see how it is created and added. – Nikolaj Klitlund Børty Sep 07 '16 at 18:48

1 Answers1

0

You'll get that error with any object you attempt to create at runtime using a parameterized constructor. If you create the object with a simple constructor (just "()") and then set all of the parameters individually, you won't run into that issue. Check the Anylogic API for specific information about the object you are using, because some require you to call .initiliaze() on that object after setting all of it's parameters if you created it using a simple constructor. Furthermore, if you want to add the object to the screen at runtime you'll need to add this code to the function that creates it:

@Override
public void onDraw( Panel panel, Graphics2D graphics) {
    obj.drawModel(panel, graphics, true);
}

where obj is replaced with the name of the object you created dynamically.

user3303925
  • 108
  • 6