Archive

Archive for March, 2011

Using ImageWidget, LabelWidget and LayerWidget in Visual Library API

March 20, 2011 Leave a comment

Here is a basic Demo of how to use ImageWidget, LabelWidget, and LayerWidget in Visual Library API.
The LayerWidget is basically like JGlassPane in SWING. Here we first add a LabelWidget and ImageWidget to LayerWidget and then add that LayerWidget to the Scene object. Hope this will be useful as a basic simple example for anyone who wants to use these Widgets.


import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.netbeans.api.visual.widget.ImageWidget;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;

/**
 *
 * @author Hassan Ali
 */


public class VisualLibraryWidgets extends JFrame {

    //instance variables

    private Scene scene = new Scene();
    private JScrollPane pane = new JScrollPane();


    //class constructor

    public VisualLibraryWidgets() {

        this.setPreferredSize(new Dimension(400, 400));
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pane.setViewportView(this.scene.createView());

        //add JScrollPane instance to JFrame instance
        this.add(pane);

    }

    //a method responsible for adding several widgets to the scene
    private void addWidgets() {

        //instantiate the LayerWidget, we would be adding other widgets
        //later in the code to this LayerWidget instance

        LayerWidget layer = new LayerWidget(this.scene);
        this.scene.addChild(layer);

        //instantiate the LabelWidget and add it to LayerWidget
        LabelWidget label = new LabelWidget(this.scene);
        label.setLabel("I am a LabelWidget!!");
        layer.addChild(label);

        //instantiate the ImageWidget and add it to the Layer
        ImageWidget image = new ImageWidget(this.scene, new ImageIcon("C:/Documents and Settings/Administrator/Desktop/hello.jpg").getImage());
        image.setToolTipText("Hi! I am an ImageWidget!!");
        layer.addChild(image);
        this.setVisible(true);


    }

    //main

    public static void main(String[] args) {

        VisualLibraryWidgets vlw = new VisualLibraryWidgets();
        vlw.addWidgets();

    }

}
A LabelWidget and ImageWidget added to a LayerWidget in a Scene object

A LabelWidget and ImageWidget added to a LayerWidget in a Scene object

Categories: Java