Feeds:
Posts
Comments

Archive for October, 2011

Few months before, just tried my own implementation of popup, where i used windows. Now i have used Popup Control. And also

i have showed how to implement a simple popup menuitem & how to show the popup. May be useful for some one else.

File Name : TestPopupMenu.java

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Popup;
import javafx.stage.Stage;

/**
 *
 * @author lawrence premkumar
 */
public class TestPopupMenu extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        Group root = new Group();
        Scene scene = new Scene(root, 800, 600, Color.AQUA);
        final VBox layout = new VBox();
        layout.getChildren().addAll(new PopupMenuItem("Lawrence").getPopupMenuItem(), new PopupMenuItem("PremKumar").getPopupMenuItem());
        Text t = new Text("Right click on the Rectangle to \n Invoke the Popup.");
        t.setFont(new Font(25));
        t.setLayoutX(20.0);
        t.setLayoutY(20.0);
        final Rectangle r = new Rectangle(200, 200);
        r.setLayoutX(100.0);
        r.setLayoutY(70.0);
        r.setFill(Color.ORANGE);
        r.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent e) {

                if (e.getButton() == javafx.scene.input.MouseButton.SECONDARY) {
                    Popup popup = new Popup();
                    popup.getContent().add(layout);
                    popup.show(r, e.getScreenX(), e.getScreenY());
                }
            }
        });
        root.getChildren().addAll(t,r);
        stage.setScene(scene);
        stage.show();
    }

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

File Name : PopupMenuItem.java

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.Label;
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  {

    Label lbl;
    Rectangle menuItemBGRectangle;

    PopupMenuItem(String name, ImageView imgView) {
        this.lbl = new Label(name , imgView);
    }

    PopupMenuItem(String name) {
        lbl = new Label(name);
    }

    public Group getPopupMenuItem(){
        Group grp = new Group();

        lbl.setLayoutX(10.0);
        menuItemBGRectangle = new Rectangle();
        menuItemBGRectangle.setWidth(200.0);
        menuItemBGRectangle.setHeight(20.0);
        menuItemBGRectangle.setFill(Color.web("#f6f6f6"));
        menuItemBGRectangle.setOnMouseEntered(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent e) {
                menuItemBGRectangle.setFill(Color.BLUE);
                lbl.setTextFill(Color.WHITE);
            }
        });

        menuItemBGRectangle.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent e) {
                menuItemBGRectangle.setFill(Color.web("#f6f6f6"));
                lbl.setTextFill(Color.BLACK);
            }
        });

        grp.getChildren().addAll(menuItemBGRectangle, lbl);
        return grp;
    }

Read Full Post »