Netbeans Visual Library Tip1: creating widget sequences from strings

a quick and easy way to create a sequence of widgets from a string is to use its split(String regex) method. It simply takes a String argument which determines the character thats used to split the actual String and returns a String Array containing single tokens. The following method takes a String in the form of: 

s:a:a:b:a:c:c:b:a:s

and creates a series connected widgets assuming that s,a,b,c  are String names of widgets created in scene.addNode(String widgetname); and a method createConnection(Widget w1, Widget w2); is implemented.

 public void createSnippetFromString(String seq) {
       String[] tokens = seq.split(“:”);
       genericWidget gw = null;
       genericWidget gw_last = null;
       Point p0 = null;
       Point p1 = new Point(0,0);
       //iterate over tokens
       for (int i=0; i<tokens.length; i++) {
           //create the widget
           gw =    (genericWidget) addNode(tokens[i]+widgetCounter);
           if (gw_last != null) p1 = gw_last.getLocation();
           else p1 = new Point(0,0);
           //adjust location
           p1.x = p1.x + 300;
           gw.setPreferredLocation(p1);
           // create connection
           if (gw_last != null) createConnection(gw_last,gw);
           gw_last = gw;
       }
   }

 

   public void createSnippetFromString(String seq) {

       String[] tokens = seq.split(“:”);

       genericWidget gw = null;

       genericWidget gw_last = null;

       Point p0 = null;

       Point p1 = new Point(0,0);

 

       //iterate over tokens

       for (int i=0; i<tokens.length; i++) {

           //create the widget

           gw =    (genericWidget) addNode(tokens[i]+widgetCounter);

           if (gw_last != null) p1 = gw_last.getLocation();

           else p1 = new Point(0,0);

 

           //adjust location

           p1.x = p1.x + 300;

           gw.setPreferredLocation(p1);

 

           // create connection

           if (gw_last != null) createConnection(gw_last,gw);

           gw_last = gw;

       }

   }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: