Creating a node using javafx.scene.layout.Container

Containers can be extended to create a custom node. Extending a container helps manage and position multiple nodes. The subclass extending the Container must override the function doLayout() of the class Container. Containers provide methods to position and specify the sizes of the node. The subclass extending the container should also override the getPrefWidth and getPrefHeight methods to make sure the layout is resized correctly. Functions provided by Container class to handle position and sizes of the node are:

  • getManaged(content:Node[]):Node[]
  • resizeNode(node:Node, width:Number, height:Number):Boolean
  • positionNode(node:Node, x:Number, y:Number):Void
  • layoutNode(node:Node, x:Number, y:Number, width:Number, height:Number):Boolean

A simple example of creating a wall of thumbnail extending container class is:


import javafx.scene.layout.Container;

public class ThumbNailContainer extends Container
{
package var imageList: String[];
package var thumbNails: ThumbNails[]; //ThumbNails is a class that
                                         //creates a thumbnail of the image.

    package function generateThumbs():Void
    {

        thumbNails = for (images in imageList)
                    {
                       ThumbNails
                       {
                          imgUrl:images;
                       }
                    }
    }

//doLayout() function overridden to create own custom layout.
 override function doLayout():Void
 {
    var x = 0;
    var y = 0;
    var temp = 0;

    generateThumbs();

    for (images in thumbNails)
    {

       setNodeWidth(images, 100);//sets the width of the node.
                                //In this case the thumbnails are the node.
       setNodeHeight(images, 100);//sets the height of the node.
                                 //In this case the thumbnails are the node
      if (x < 600)
      {
          positionNode(images, x += temp, y); //positions the node
          temp = 110;
      }
      else
      {
         x = 0;
         temp = 0;
         y += 110;
      }
 }

 content = thumbNails;//sets the content as the sequeces of thumbnails
 }


More info: http://download.oracle.com/docs/cd/E17802_01/javafx/javafx/1.3/docs/api/javafx.scene.layout/javafx.scene.layout.Container.html

 

 

Advertisements