Feeds:
Posts
Comments

Archive for June, 2011


Popup is special window(no decorations with a null fill), like container for a scene graph which is seen above the stage and its invoked when user right clicks on the Node. This blog is an initial draft for the special popup i,e this SpecialPopup is different from the usual popup. SpecialPopup can be created like matrix. This SpecialPopup is created using TilePane for laying out the individual PopupMenuItem. I will develop this initial draft of this control as a full fluged control going forward.
Note : Image used for this example

SpecialPopup image is missingFilename : PopupMenuItem.java

package popup;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 *
 * @author Lawrence PremKumar
 */
public class PopupMenuItem extends Group{

    public Label popUpMenuItem  ;
    final Rectangle bgRect = new Rectangle();

    PopupMenuItem(){
        popUpMenuItem = new Label();
    }
    PopupMenuItem(String menuName ,  javafx.scene.control.ContentDisplay labelPos ,ImageView imgView){
        popUpMenuItem = new Label(menuName,imgView);
        popUpMenuItem.setContentDisplay(labelPos);
        popUpMenuItem.setTextFill(Color.WHITE);
    }

    public Group createPopupMenuItem(){

        this.setOnMouseEntered(new EventHandler() {
            public void handle(MouseEvent event) {
                 bgRect.setWidth(popUpMenuItem.getWidth()+2);
                 bgRect.setHeight(popUpMenuItem.getHeight()+2);
                 bgRect.setArcHeight(5.0);
                 bgRect.setArcWidth(5.0);
                 bgRect.setFill(Color.BLACK);
                 bgRect.setOpacity(0.5);
            }
        });

        this.setOnMouseExited(new EventHandler() {
            public void handle(MouseEvent event) {
                  bgRect.setOpacity(0.0);
                  System.out.println("Mouse exited \n");
            }
        });

        this.getChildren().addAll(popUpMenuItem,bgRect);
        return this;
    }
}

Filename : PopupMenu.java

 
package popup; 
import javafx.geometry.Orientation; 
import javafx.scene.layout.TilePane; 
 
 /*
  * @author Lawrence PremKumar 
  *
 */ 
  final public class PopupMenu extends TilePane {
    PopupMenu() {
    }
    PopupMenu(double hgap, double vgap) {
        super(hgap, vgap);
    }

    public boolean add(java.util.ArrayList popupArray) {
        boolean flag = true;
        for (int i = 0; i <= popupArray.size();i++) { 
	flag = this.getChildren().add(popupArray.get(i).createPopupMenuItem());
            if (!flag) {
                break;
            } else {
                continue;
            }
        }
        return flag;
    }
}

Filename : TestPopup.java

package popup; 
import javafx.application.Application; 
import javafx.event.EventHandler; 
import javafx.geometry.Orientation; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.control.ContentDisplay; 
import javafx.scene.effect.DropShadow; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.input.MouseButton; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.TilePane;
import javafx.scene.paint.Color; 
import javafx.scene.paint.CycleMethod; 
import javafx.scene.paint.LinearGradient; 
import javafx.scene.paint.Stop; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
import javafx.stage.StageStyle; 

/** 
 * 
 * @author Lawrence PremKumar 
 */

public class TestPopup extends Application {

    Stage stage1 = null;
    PopupMenuItem1 menu1 = null;
    PopupMenuItem1 menu2 = null;
    PopupMenuItem1 menu3 = null;
    PopupMenuItem1 menu4 = null;

    public static void main(String[] args) {
        Application.launch(TestPopup.class, args);
    }

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250);
        ImageView imgView = new ImageView();
        imgView.setFitHeight(40.0);
        imgView.setFitWidth(40.0);
        Image img = new Image(TestPopupControl.class.getResourceAsStream("duke.jpg"));
        imgView.setImage(img);
        Image img1 = new Image(TestPopupControl.class.getResourceAsStream("duke.jpg"));
        ImageView imgView1 = new ImageView();
        imgView1.setFitHeight(40.0);
        imgView1.setFitWidth(40.0);
        imgView1.setImage(img1);
        Image img2 = new Image(TestPopupControl.class.getResourceAsStream("duke.jpg"));
        ImageView imgView2 = new ImageView();
        imgView2.setFitHeight(40.0);
        imgView2.setFitWidth(40.0);
        imgView2.setImage(img2);
        menu1 = new PopupMenuItem1("One", ContentDisplay.TOP, imgView);
        menu2 = new PopupMenuItem1("Two", ContentDisplay.TOP, imgView1);
        menu3 = new PopupMenuItem1("Three", ContentDisplay.TOP, imgView2);
        menu1.setOnMouseClicked(new MouseEventHandler());
        menu2.setOnMouseClicked(new MouseEventHandler());
        menu3.setOnMouseClicked(new MouseEventHandler());
        final java.util.ArrayList popupArray = new java.util.ArrayList();
        popupArray.add(menu1);
        popupArray.add(menu2);
        popupArray.add(menu3);
        Rectangle bgRect = new Rectangle();
        bgRect.setWidth(200);
        bgRect.setHeight(130);
        bgRect.setArcHeight(5.0);
        bgRect.setArcWidth(5.0);
        bgRect.setFill(Color.RED);
        final PopupMenu popup = new PopupMenu(5, 5);
        popup.setHgap(5.0);
        popup.setLayoutX(5.0);
        popup.setLayoutY(7.0);
        popup.setPrefColumns(3);
        popup.setPrefRows(1);
        popup.add(popupArray);
        bgRect.setOnMouseClicked(new EventHandler() {
            public void handle(MouseEvent event) {
                if (event.getButton() == MouseButton.SECONDARY) {
                    stage1 = new Stage(StageStyle.TRANSPARENT);
                    Group rootGroup = new Group();
                    DropShadow ds = new DropShadow();
                    rootGroup.setEffect(ds);
                    ds.setOffsetY(3.0f);
                    ds.setColor(Color.color(0.4f, 0.4f, 0.4f));
                    Scene scene = new Scene(rootGroup);
                    scene.setFill(null);
                    Rectangle r = new Rectangle();
                    r.setWidth(140);
                    r.setHeight(70);
                    Stop[] stops = new Stop[]{new Stop(0, Color.web("#1F6592")), new Stop(1, Color.web("#80CAFA"))};
                    LinearGradient lg = new LinearGradient(125.0, 0.0, 225.0, 0.0, true, CycleMethod.NO_CYCLE, stops);
                    r.setFill(lg);
                    r.setArcHeight(10.0);
                    r.setArcWidth(10.0);
                    rootGroup.getChildren().addAll(r, popup);
                    stage1.setScene(scene);
                    stage1.setX(event.getScreenX());
                    stage1.setY(event.getScreenY());
                    stage1.setVisible(true);
                }
            }
        });
        root.getChildren().addAll(bgRect);
        primaryStage.setScene(scene);
        primaryStage.setVisible(true);
    }

    public class MouseEventHandler implements EventHandler {
        public void handle(MouseEvent me) {
            if (me.getSource().equals(menu1)) { 
                // Action specific to menu1 
                stage1.close(); 
            } 
            if(me.getSource().equals(menu2)){ 
                // Action specific to menu2 
                stage1.close(); 
            } 
            if(me.getSource().equals(menu3)){ 
                // Action specific to menu3 
                stage1.close(); 
            } 
        } 
    } 
}
Advertisements

Read Full Post »

Tried creating a simple “Current Date Widget” using JavaFX 2.0.

Datewidget image file not available

While trying to develop current Date widget came to know that right now its not possible to create a main stage as undecorated. This is because, JavaFX 2.0 dn’t have stage.setStyle() method. But other than the primary stage or main Stage, all other stages can be created as undecorated or decorated depending upon the users choice.

First let me show you how to close the primary stage without user interaction.Add a button to the stage and call the button.fire(); this method arms the button & triggers the ActionEvent.
Note:- According to the doc on fire(), “This function is intended to be used by experts, primarily by those implementing new Skins or Behaviors. It is not common for developers or designers to access this function directly.”

Stage is closed using stage.close() method.
Stage(StageStyle.UNDECORATED) is used to create a undecorated stage.

“To make it more dynamic, i need to implement a timer class and update the date very 5 minutes or to some nth min.”


import java.util.Calendar;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Light.Distant;
import javafx.scene.effect.Lighting;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
*
* @author Lawrence PremKumar
*/

public class TodaysDateWiget extends Application {

	final double WIDTH = 150.0;
	final double HEIGHT = 150.0;

	public static void main(String[] args) {
		Application.launch(TodaysDateWiget.class, args);
	}

	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("Hello World");
		Group root = new Group();
		Scene scene = new Scene(root, 300, 250, Color.LIGHTGREEN);
		Button btn = new Button();
		btn.setLayoutX(100);
		btn.setLayoutY(80);
		btn.setText("Hello World");
		btn.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
			public void handle(ActionEvent event) {
				Stage st = new Stage(StageStyle.UNDECORATED);
				Group group = new Group();
				Scene s = new Scene(group ,WIDTH+3,HEIGHT+5);
				s.setFill(null);
				group.getChildren().addAll(getToDayControl());
				st.setScene(s);
				st.setVisible(true);

			}
		});
		btn.fire();
		root.getChildren().add(btn);
		primaryStage.setScene(scene);
		primaryStage.setVisible(false);
		primaryStage.close();
	}

	public Group getToDayControl(){
		String[] months = {"Jan", "Feb","Mar", "Apr", "May",          "Jun", "Jul","Aug", "Sep", "Oct", "Nov","Dec"};
		Calendar cal =  Calendar.getInstance();

		Group ctrl = new Group();
		Rectangle rect = new Rectangle();
		rect.setWidth(WIDTH);
		rect.setHeight(HEIGHT);
		rect.setArcHeight(10.0);
		rect.setArcWidth(10.0);

		Rectangle headerRect = new Rectangle();
		headerRect.setWidth(WIDTH);
		headerRect.setHeight(30);
		headerRect.setArcHeight(10.0);
		headerRect.setArcWidth(10.0);

		Stop[] stops = new Stop[] { new Stop(0, Color.color(0.31, 0.31, 0.31, 0.443)), new Stop(1,  Color.color(0, 0, 0, 0.737))};
		LinearGradient lg = new LinearGradient( 0.482, -0.017, 0.518, 1.017, true, CycleMethod.REFLECT, stops);
		headerRect.setFill(lg);

		Rectangle footerRect = new Rectangle();
		footerRect.setY(headerRect.getBoundsInLocal().getHeight() -4);
		footerRect.setWidth(WIDTH);
		footerRect.setHeight(125);
		footerRect.setFill(Color.color(0.51,  0.671,  0.992));

		final Text currentMon = new Text(months[(cal.get(Calendar.MONTH) )]);
		currentMon.setFont(Font.font("null", FontWeight.BOLD, 24));
		currentMon.setTranslateX((footerRect.getBoundsInLocal().getWidth() - currentMon.getBoundsInLocal().getWidth())/2.0);
		currentMon.setTranslateY(23);
		currentMon.setFill(Color.WHITE);

		final Text currentDate = new          Text(Integer.toString(cal.get(Calendar.DATE)));
		currentDate.setFont(new Font(100.0));
		currentDate.setTranslateX((footerRect.getBoundsInLocal().getWidth() - currentDate.getBoundsInLocal().getWidth())/2.0);
		currentDate.setTranslateY(120);
		currentDate.setFill(Color.WHITE);

		ctrl.getChildren().addAll(rect, headerRect, footerRect , currentMon,currentDate);

		DropShadow ds = new DropShadow();
		ds.setOffsetY(3.0);
		ds.setOffsetX(3.0);
		ds.setColor(Color.GRAY);
		ctrl.setEffect(ds);

		return ctrl;
	}
}

Read Full Post »

TabPane is one of the most commonly used component in any client side application. This blog shows you how to create a TabPane in JavaFX 2.0 and how to change its side dynamically apart from this it even shows you how to create and use the RadioButton.

 

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
*
* @author Lawrence PremKumar
*/
public class TabPaneExample extends Application {
	TabPane tabPane = null;
	ChoiceBox cb = new ChoiceBox();
	Color colors[] = {Color.BLUE, Color.RED, Color.GREEN, Color.GOLD};

	@Override
	public void start(Stage stage) throws Exception {
		stage.setTitle("Tab Example");
		Group root = new Group();
		Scene scene = new Scene(root, 500, 500);</p>

	     	// create a TabPane instance
		tabPane = new TabPane();
		tabPane.setLayoutX(20);
		tabPane.setLayoutY(20);

		// create individual tabs
		for (int i = 0; i &lt; colors.length; i++) {
			Tab tab = new Tab();
			// set the label for tab
			tab.setText("Tab # " + (i + 1));

			// add the content to tab
			tab.setContent(new Rectangle(400, 400, colors[i]));

			// add the tab to TabPane
			tabPane.getTabs().add(tab);
		}

		//create a hbox to hold the radio buttons
		HBox sideLayout = new HBox();
		sideLayout.setSpacing(10);

		final ToggleGroup tg = new ToggleGroup();
		RadioButton top = new RadioButton("Top");
		top.setSelected(true);
		top.setUserData("Top");
		top.setToggleGroup(tg);

		RadioButton bottom = new RadioButton("Bottom");
		bottom.setToggleGroup(tg);
		bottom.setUserData("Bottom");

		RadioButton left = new RadioButton("Left");
		left.setToggleGroup(tg);
		left.setUserData("Left");

		RadioButton right = new RadioButton("Right");
		right.setToggleGroup(tg);
		right.setUserData("Right");

		sideLayout.getChildren().addAll(top, bottom, left, right);

		// Add change listener to toggleGroup
		tg.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {

			public void changed(ObservableValue< extends Toggle> observable,
					Toggle oldValue, Toggle newValue) {
				String selectedValue = tg.getSelectedToggle().getUserData().toString();
				System.out.println(selectedValue);
				if (selectedValue.equals("Top")) {
					tabPane.setSide(Side.TOP);
				}
				if (selectedValue.equals("Bottom")) {
					tabPane.setSide(Side.BOTTOM);
				}
				if (selectedValue.equals("Left")) {
					tabPane.setSide(Side.LEFT);
				}
				if (selectedValue.equals("Right")) {
					tabPane.setSide(Side.RIGHT);
				}
			}
		});

		root.getChildren().addAll(sideLayout, tabPane);
		stage.setScene(scene);
		stage.setVisible(true);
	}

	public static void main(String[] args) {
		Application.launch(TabPaneExample.class, args);
	}
}

Read Full Post »