Tuesday, February 28, 2012

JavaFX 2.0: TranslateTransition



package javafx_imagetransition;

import javafx.animation.*;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ImageTransition extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    Scene scene;
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("http://java-buddy.blogspot.com/");
        StackPane root = new StackPane();
        scene = new Scene(root, 300, 250);
        
        VBox vBox = new VBox();
        vBox.setSpacing(3);
        
        final Image image1 = new Image(getClass().getResourceAsStream("duke_44x80.png"));
        final ImageView imageView = new ImageView();
        imageView.setImage(image1);
        
        final TranslateTransition transitionForward 
                = createTransitionForward(imageView);
        final TranslateTransition transitionBackward 
                = createTransitionBackward(imageView);
 
        Button btnForward = new Button();
        btnForward.setText("Forward");
        btnForward.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                transitionForward.play();
            }
        });
        
        Button btnBackward = new Button();
        btnBackward.setText("Backward");
        btnBackward.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                transitionBackward.play();
            }
        });
        
        vBox.getChildren().add(imageView);
        vBox.getChildren().add(btnForward);
        vBox.getChildren().add(btnBackward);
        
        root.getChildren().add(vBox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    TranslateTransition createTransitionForward(final ImageView iv){
        TranslateTransition transition = TranslateTransitionBuilder.create()
                .node(iv)
                .fromX(0)
                .toX(scene.widthProperty().doubleValue() - iv.getImage().getWidth())
                .fromY(0)
                .toY(scene.heightProperty().doubleValue() - iv.getImage().getHeight())
                .duration(Duration.millis(1000))
                .interpolator(Interpolator.LINEAR)
                .cycleCount(1)
                .build();
        
        return transition;
    }
    
    TranslateTransition createTransitionBackward(final ImageView iv){
        TranslateTransition transition = TranslateTransitionBuilder.create()
                .node(iv)
                .fromX(scene.widthProperty().doubleValue() - iv.getImage().getWidth())
                .toX(0)
                .fromY(scene.heightProperty().doubleValue() - iv.getImage().getHeight())
                .toY(0)
                .duration(Duration.millis(1000))
                .interpolator(Interpolator.LINEAR)
                .cycleCount(1)
                .build();
        
        return transition;
    }
}



Read more: Implement setOnFinished() to handle end of Transition

Monday, February 27, 2012

JavaFX 2.0: Image transition effect



package javafx_imagetransition;

import javafx.animation.FadeTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.SequentialTransitionBuilder;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_ImageTransition extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("http://java-buddy.blogspot.com/");
        
        HBox hBox = new HBox();
        hBox.setSpacing(3);
        
        final Image image1 = new Image(getClass().getResourceAsStream("duke_44x80.png"));
        final Image image2 = new Image(getClass().getResourceAsStream("duke_44x80_flip.png"));
        final ImageView imageView = new ImageView();
        imageView.setImage(image1);
        
        final SequentialTransition transitionForward = createTransition(imageView, image2);
        final SequentialTransition transitionBackward = createTransition(imageView, image1);

 
        Button btnForward = new Button();
        btnForward.setText("Forward");
        btnForward.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                transitionForward.play();
            }
        });
        
        Button btnBackward = new Button();
        btnBackward.setText("Backward");
        btnBackward.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                transitionBackward.play();
            }
        });
        
        hBox.getChildren().add(imageView);
        hBox.getChildren().add(btnForward);
        hBox.getChildren().add(btnBackward);
        StackPane root = new StackPane();
        root.getChildren().add(hBox);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }
    
    SequentialTransition createTransition(final ImageView iv, final Image img){
        FadeTransition fadeOutTransition 
                        = new FadeTransition(Duration.seconds(1), iv);
                fadeOutTransition.setFromValue(1.0);
                fadeOutTransition.setToValue(0.0);
                fadeOutTransition.setOnFinished(new EventHandler<ActionEvent>(){

                    @Override
                    public void handle(ActionEvent arg0) {
                        iv.setImage(img);;
                    }
                    
                });
                
                FadeTransition fadeInTransition 
                        = new FadeTransition(Duration.seconds(1), iv);
                fadeInTransition.setFromValue(0.0);
                fadeInTransition.setToValue(1.0);
                SequentialTransition sequentialTransition 
                        = SequentialTransitionBuilder
                        .create()
                        .children(fadeOutTransition, fadeInTransition)
                        .build();
                
                return sequentialTransition;
    }
}


Sunday, February 26, 2012

JavaFX 2.0: Implement Fade In/Fade Out animation

This exercise implement OnMouseEntered and OnMouseExited EventHandler, to apply Fade In and Fade Out animation on a button when mouse move in and move out the scene.

package javafx_fadeinout;

import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_FadeInOut extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("http://java-buddy.blogspot.com/");
        final Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello World!");
            }
        });
        
        StackPane root = new StackPane();
        root.getChildren().add(btn);
        Scene scene = new Scene(root, 300, 250);
        
        scene.setOnMouseEntered(new EventHandler<MouseEvent>(){

            public void handle(MouseEvent mouseEvent){
                FadeTransition fadeTransition 
                        = new FadeTransition(Duration.millis(500), btn);
                fadeTransition.setFromValue(0.0);
                fadeTransition.setToValue(1.0);
                fadeTransition.play();
            }
        });
        
        scene.setOnMouseExited(new EventHandler<MouseEvent>(){

            public void handle(MouseEvent mouseEvent){
                FadeTransition fadeTransition 
                        = new FadeTransition(Duration.millis(500), btn);
                fadeTransition.setFromValue(1.0);
                fadeTransition.setToValue(0.0);
                fadeTransition.play();
            }
        });
        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}


Friday, February 24, 2012

Java SE 7 Language and JVM Specification available now


Java SE 7 Edition Language Specification and the Java Virtual Machine Specification available, in PDF and HTML format.

Download it: http://docs.oracle.com/javase/specs/index.html

Oracle Now: a free mobile application, stay connected to the latest Oracle news, events, video, and more.

Download the Oracle mobile application, a free mobile application, and stay connected to the latest Oracle news, events, video, and more.

  • News—Customizable filters let you track Oracle news specific to your interests
  • Events—Experience Oracle OpenWorld, JavaOne, and Oracle Develop on your mobile device, plus learn about and register for Oracle events worldwide
  • Webcasts—Discover more about Oracle technologies through expert-led webcasts
  • Blogs—Participate in an on-going dialog with your favorite Oracle bloggers
  • QR Code Reader—Scan QR codes for instant information on your mobile device.
  • Video*—See clips of Webcasts, executive addresses and keynotes, special technology series, and much, much more.
*Video for iPhone is available only on iPhone 3GS and 4. Video for BlackBerry is coming soon.

Oracle Now is free to download:



Source: http://www.oracle.com/us/corporate/mobile-application-171155.html

Modality of Stage


javafx.stage.Modality defines the possible modality types for a Stage.
  • APPLICATION_MODAL
    Defines a modal window that blocks events from being delivered to any other application window.
  • NONE
    Defines a top-level window that is not modal.
  • WINDOW_MODAL
    Defines a modal window that block events from being delivered to its entire owner window hierarchy.

Modify the code of last article "Dialog with CLOSE button", try difference Modality to check the effect.
package javafx_mydialog;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_MyDialog extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");
Button btn = new Button();
btn.setText("Open Dialog");
btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
final Stage myDialog = new Stage();
//myDialog.initModality(Modality.WINDOW_MODAL);
//myDialog.initModality(Modality.NONE);
myDialog.initModality(Modality.APPLICATION_MODAL);

Button okButton = new Button("CLOSE");
okButton.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
myDialog.close();
}

});

Scene myDialogScene = new Scene(VBoxBuilder.create()
.children(new Text("Hello! it's My Dialog."), okButton)
.alignment(Pos.CENTER)
.padding(new Insets(10))
.build());

myDialog.setScene(myDialogScene);
myDialog.show();
}
});

StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

Thursday, February 23, 2012

Dialog with CLOSE button

Last article, Create Dialog using Stage, demonstrate how to create our dialog by instancing object from Stage class. Normally, we have a button to close the dialog. In this article, a okButton is implemented to close the dialog.
Dialog with CLOSE button
package javafx_mydialog;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_MyDialog extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");
Button btn = new Button();
btn.setText("Open Dialog");
btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
final Stage myDialog = new Stage();
myDialog.initModality(Modality.WINDOW_MODAL);

Button okButton = new Button("CLOSE");
okButton.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
myDialog.close();
}

});

Scene myDialogScene = new Scene(VBoxBuilder.create()
.children(new Text("Hello! it's My Dialog."), okButton)
.alignment(Pos.CENTER)
.padding(new Insets(10))
.build());

myDialog.setScene(myDialogScene);
myDialog.show();
}
});

StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}


Next:
- Modality of Stage

Create Dialog using Stage

There are no standard Dialog in JavaFX 2.0. You can create your own dialog using javafx.stage.Stage.
Create Dialog using Stage
package javafx_mydialog;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_MyDialog extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");
Button btn = new Button();
btn.setText("Open Dialog");
btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
Stage myDialog = new Stage();
myDialog.initModality(Modality.WINDOW_MODAL);

Scene myDialogScene = new Scene(VBoxBuilder.create()
.children(new Text("Hello! it's My Dialog."))
.alignment(Pos.CENTER)
.padding(new Insets(10))
.build());

myDialog.setScene(myDialogScene);
myDialog.show();
}
});

StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}


More:
- Dialog with CLOSE button

Wednesday, February 22, 2012

JavaFX 2.0 TabPane: Set the position to place the tabs

JavaFX 2.0 TabPane: Set the position to place the tabs
package javafx_uitabpane;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Side;

import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_uiTabPane extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

final TabPane tabPane = new TabPane();
BorderPane mainPane = new BorderPane();

//Create Tabs
Tab tabA = new Tab();
tabA.setText("Tab A");
//Add something in Tab
Button tabA_button = new Button("Button@Tab A");
tabA.setContent(tabA_button);
tabPane.getTabs().add(tabA);

Tab tabB = new Tab();
tabB.setText("Tab B");
//Add something in Tab
StackPane tabB_stack = new StackPane();
tabB_stack.setAlignment(Pos.CENTER);
tabB_stack.getChildren().add(new Label("Label@Tab B"));
tabB.setContent(tabB_stack);
tabPane.getTabs().add(tabB);

Tab tabC = new Tab();
tabC.setText("Set Tab Side");
//Add something in Tab
VBox tabC_vBox = new VBox();

Button ButtonLeft = new Button("Set Tab on Left");
ButtonLeft.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
tabPane.setSide(Side.LEFT);
}

});

Button ButtonTop = new Button("Set Tab on Top");
ButtonTop.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
tabPane.setSide(Side.TOP);
}

});

Button ButtonRight = new Button("Set Tab on Right");
ButtonRight.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
tabPane.setSide(Side.RIGHT);
}

});

Button ButtonBottom = new Button("Set Tab on Bottom");
ButtonBottom.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
tabPane.setSide(Side.BOTTOM);
}

});

tabC_vBox.getChildren().addAll(
ButtonLeft,
ButtonTop,
ButtonRight,
ButtonBottom);
tabC.setContent(tabC_vBox);
tabPane.getTabs().add(tabC);

mainPane.setCenter(tabPane);

mainPane.prefHeightProperty().bind(scene.heightProperty());
mainPane.prefWidthProperty().bind(scene.widthProperty());

root.getChildren().add(mainPane);
primaryStage.setScene(scene);
primaryStage.show();
}

}


Saturday, February 18, 2012

JavaFX 2.0: Add content to Tabs

Add content to Tabs
package javafx_uitabpane;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;

import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_uiTabPane extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

TabPane tabPane = new TabPane();
BorderPane mainPane = new BorderPane();

//Create Tabs
Tab tabA = new Tab();
tabA.setText("Tab A");
//Add something in Tab
Button tabA_button = new Button("Button@Tab A");
tabA.setContent(tabA_button);
tabPane.getTabs().add(tabA);

Tab tabB = new Tab();
tabB.setText("Tab B");
//Add something in Tab
StackPane tabB_stack = new StackPane();
tabB_stack.setAlignment(Pos.CENTER);
tabB_stack.getChildren().add(new Label("Label@Tab B"));
tabB.setContent(tabB_stack);
tabPane.getTabs().add(tabB);

Tab tabC = new Tab();
tabC.setText("Tab C");
//Add something in Tab
VBox tabC_vBox = new VBox();
tabC_vBox.getChildren().addAll(
new Button("Button 1@Tab C"),
new Button("Button 2@Tab C"),
new Button("Button 3@Tab C"),
new Button("Button 4@Tab C"));
tabC.setContent(tabC_vBox);
tabPane.getTabs().add(tabC);

mainPane.setCenter(tabPane);

mainPane.prefHeightProperty().bind(scene.heightProperty());
mainPane.prefWidthProperty().bind(scene.widthProperty());

root.getChildren().add(mainPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFX 2.0: TabPane

Example of basic TabPane for JavaFX 2.0
JavaFX 2.0: TabPane
package javafx_uitabpane;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_uiTabPane extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

TabPane tabPane = new TabPane();
BorderPane mainPane = new BorderPane();

//Create Tabs
Tab tabA = new Tab();
tabA.setText("Tab A");
tabPane.getTabs().add(tabA);

Tab tabB = new Tab();
tabB.setText("Tab B");
tabPane.getTabs().add(tabB);

Tab tabC = new Tab();
tabC.setText("Tab C");
tabPane.getTabs().add(tabC);

mainPane.setCenter(tabPane);

mainPane.prefHeightProperty().bind(scene.heightProperty());
mainPane.prefWidthProperty().bind(scene.widthProperty());

root.getChildren().add(mainPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}


next:
- Add content to Tabs

JavaFX 2.0: Set Accelerator (KeyCombination) for menu items

Using setAccelerator(KeyCombination value) method, you can assign menu accelerator, a key combination that performs the same action as the menu item.

Example:
Set Accelerator for menu items
package javafx_exmenu;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCombination;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_exMenu extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

//Top Menu Bar
MenuBar menuBar = new MenuBar();

Menu menu1 = new Menu("Menu");

MenuItem menuItemA = new MenuItem("Item A");
menuItemA.setAccelerator(KeyCombination.keyCombination("Ctrl+A"));
menuItemA.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Item A Clicked");
}
});

MenuItem menuItemB = new MenuItem("Item B");
menuItemB.setAccelerator(KeyCombination.keyCombination("Ctrl+B"));
menuItemB.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Item B Clicked");
}
});

MenuItem menuItemC = new MenuItem("Item C");
menuItemC.setAccelerator(KeyCombination.keyCombination("Ctrl+C"));
menuItemC.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Item C Clicked");
}
});

menu1.getItems().add(menuItemA);
menu1.getItems().add(menuItemB);
menu1.getItems().add(menuItemC);
menuBar.getMenus().add(menu1);

menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
}


Related:
- JavaFX 2.0: Add menu

Thursday, February 16, 2012

JavaFX 2.0: ProgressBar and ProgressIndicator

Example of ProgressBar in JavaFX 2.0. You can try clicking on the "+" and "-" buttons to change the progress to greater than 100% and lower than 0%, to view how it show on ProgressBar and ProgressIndicator.
JavaFX 2.0: ProgressBar and ProgressIndicator
package javafx_progressbar;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_ProgressBar extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");

VBox root = new VBox();
HBox hBox1 = new HBox();
HBox hBox2 = new HBox();
HBox hBox3 = new HBox();
hBox1.setSpacing(3);
hBox2.setSpacing(3);
hBox3.setSpacing(3);

//Without set progress
ProgressBar myProgressBar1 = new ProgressBar();
ProgressIndicator myProgressIndicator1 = new ProgressIndicator();
myProgressIndicator1.setProgress(myProgressBar1.getProgress());
hBox1.getChildren().add(myProgressBar1);
hBox1.getChildren().add(myProgressIndicator1);

//With fixed progress
ProgressBar myProgressBar2 = new ProgressBar();
myProgressBar2.setProgress(0.3);
ProgressIndicator myProgressIndicator2 = new ProgressIndicator();
myProgressIndicator2.setProgress(myProgressBar2.getProgress());
hBox2.getChildren().add(myProgressBar2);
hBox2.getChildren().add(myProgressIndicator2);

//+/- progress
final ProgressBar myProgressBar3 = new ProgressBar();
myProgressBar3.setProgress(0.1);
final ProgressIndicator myProgressIndicator3 = new ProgressIndicator();
myProgressIndicator3.setProgress(myProgressBar3.getProgress());

Button buttonInc = new Button("+");
Button buttonDec = new Button("-");

buttonInc.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
myProgressBar3.setProgress(myProgressBar3.getProgress() + 0.1);
myProgressIndicator3.setProgress(myProgressBar3.getProgress());
}
});

buttonDec.setOnAction(new EventHandler<ActionEvent>(){

@Override
public void handle(ActionEvent arg0) {
myProgressBar3.setProgress(myProgressBar3.getProgress() - 0.1);
myProgressIndicator3.setProgress(myProgressBar3.getProgress());
}
});

hBox3.getChildren().add(buttonDec);
hBox3.getChildren().add(myProgressBar3);
hBox3.getChildren().add(myProgressIndicator3);
hBox3.getChildren().add(buttonInc);

root.getChildren().add(hBox1);
root.getChildren().add(hBox2);
root.getChildren().add(hBox3);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}


Wednesday, February 15, 2012

Java Performance


Java Performance covers the latest Oracle and third-party tools for monitoring and measuring performance on a wide variety of hardware architectures and operating systems. The authors present dozens of tips and tricks you’ll find nowhere else.


You’ll learn how to construct experiments that identify opportunities for optimization, interpret the results, and take effective action. You’ll also find powerful insights into microbenchmarking–including how to avoid common mistakes that can mislead you into writing poorly performing software. Then, building on this foundation, you’ll walk through optimizing the Java HotSpot VM, standard and multitiered applications; Web applications, and more. Coverage includes

  • Taking a proactive approach to meeting application performance and scalability goals
  • Monitoring Java performance at the OS level in Windows, Linux, and Oracle Solaris environments
  • Using modern Java Virtual Machine (JVM) and OS observability tools to profile running systems, with almost no performance penalty
  • Gaining “under the hood” knowledge of the Java HotSpot VM that can help you address most Java performance issues
  • Integrating JVM-level and application monitoring
  • Mastering Java method and heap (memory) profiling
  • Tuning the Java HotSpot VM for startup, memory footprint, response time, and latency
  • Determining when Java applications require rework to meet performance goals
  • Systematically profiling and tuning performance in both Java SE and Java EE applications
  • Optimizing the performance of the Java HotSpot VM


Using this book, you can squeeze maximum performance and value from all your Java applications–no matter how complex they are, what platforms they’re running on, or how long you’ve been running them.

JavaFX 2.0: Implement ChangeListener for Slider

JavaFX 2.0: Implement ChangeListener for Slider
package javafx_uislider;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_uiSlider extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");

VBox root = new VBox();

Slider mySlider = new Slider();
mySlider.setMin(0);
mySlider.setMax(100);
mySlider.setValue(50);
mySlider.setShowTickLabels(true);
mySlider.setShowTickMarks(true);
mySlider.setMajorTickUnit(50);
mySlider.setMinorTickCount(5);
mySlider.setBlockIncrement(10);

final Label mySliderValue = new Label(Double.toString(mySlider.getValue()));

mySlider.valueProperty().addListener(new ChangeListener<Number>() {

@Override
public void changed(ObservableValue<? extends Number> arg0,
Number arg1, Number arg2) {
mySliderValue.setText(String.format("%.1f", arg2)); //new value
}

});

root.getChildren().add(mySlider);
root.getChildren().add(mySliderValue);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

Basic Slider of JavaFX 2.0

Basic Slider of JavaFX 2.0
package javafx_uislider;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_uiSlider extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");

VBox root = new VBox();

Slider mySlider = new Slider();
mySlider.setMin(0);
mySlider.setMax(100);
mySlider.setValue(50);
mySlider.setShowTickLabels(true);
mySlider.setShowTickMarks(true);
mySlider.setMajorTickUnit(50);
mySlider.setMinorTickCount(5);
mySlider.setBlockIncrement(10);

root.getChildren().add(mySlider);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

Monday, February 13, 2012

Set JavaFX ListView orientation in horizontal

To set ListView orientation in horizontal, using the code:
listView.setOrientation(Orientation.HORIZONTAL);
Set JavaFX ListView orientation in horizontal
package javafx_ui;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_ui extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("http://java-buddy.blogspot.com/");

ListView<String> listView = new ListView<>();
ObservableList<String> list =FXCollections.observableArrayList (
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday");
listView.setItems(list);
listView.setOrientation(Orientation.HORIZONTAL);

StackPane root = new StackPane();
root.getChildren().add(listView);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

Implement ListView using JavaFX

Implement ListView using JavaFX
package javafx_ui;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_ui extends Application {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
      primaryStage.setTitle("http://java-buddy.blogspot.com/");
    
      ListView<String> listView = new ListView<>();
      ObservableList<String> list =FXCollections.observableArrayList (
              "Sunday",
              "Monday",
              "Tuesday",
              "Wednesday",
              "Thursday",
              "Friday",
              "Saturday");
      listView.setItems(list);
    
      StackPane root = new StackPane();
      root.getChildren().add(listView);
      primaryStage.setScene(new Scene(root, 300, 250));
      primaryStage.show();
  }
}


Related:
- Implement OnMouseClicked EventHandler for ListView
- Implement JavaFX ListView for custom object

JavaFX 2.0: set width and height of Layout Pane - setPrefSize()

with borderPane.setPrefSize()
without borderPane.setPrefSize()
package javafx_layoutpanes;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_LayoutPanes extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com:");
Group root = new Group();
Scene scene = new Scene(root, 400, 300);

BorderPane borderPane = new BorderPane();
borderPane.setPrefSize(scene.getWidth(), scene.getHeight());

borderPane.setTop(new Button("Top"));
borderPane.setBottom(new Button("Bottom"));
borderPane.setCenter(new Button("Center"));
borderPane.setLeft(new Button("Left"));
borderPane.setRight(new Button("Right"));

root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}


Sunday, February 12, 2012

JavaFX 2.0: Full Screen scene

To make the scene occupy full screen, use the code:
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());


JavaFX 2.0: Full Screen scene
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javafx_fullscreen;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
*
* @author erix7
*/
public class JavaFX_FullScreen extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {

@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});

StackPane root = new StackPane();

//Make scene occupy full screen
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, screenBounds.getWidth(), screenBounds.getHeight());

root.getChildren().add(btn);
primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFX 2.0: GridPane

JavaFX 2.0: GridPane
package javafx_layoutpanes;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_LayoutPanes extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com:");
Group root = new Group();

double width = 400;
double height = 300;

Scene scene = new Scene(root, width, height, Color.WHITE);

GridPane gridPane = new GridPane();

gridPane.add(new Text("0, 0"), 0, 0);
gridPane.add(new Button("0, 1"), 0, 1);
gridPane.add(new Text("1, 1"), 1, 1);
gridPane.add(new Button("1, 2"), 1, 2);
gridPane.add(new Button("1, 3"), 1, 3);
gridPane.add(new Button("2,3"), 2, 3);
gridPane.add(new Button("4, 0"), 4, 0);
gridPane.add(new Text("4, 2"), 4, 2);

root.getChildren().add(gridPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}


Saturday, February 11, 2012

JavaFX 2.0: Apply css style

JavaFX 2.0: Apply css style
package javafx_image;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.control.Button;
import javafx.stage.Stage;

/**
*
* @web http://mobile-web-app.blogspot.com/
*/
public class JavaFX_image extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com: Load image");
Group root = new Group();

double width = 400;
double height = 300;
Scene scene = new Scene(root, width, height, Color.WHITE);

Image image = new Image(getClass().getResourceAsStream("duke_44x80.png"));
ImageView imageView = new ImageView();
imageView.setImage(image);

VBox vBox = new VBox();
Button button = new Button("it's a Button");
Text text = new Text("css style applied on root!");

String style_root = "-fx-font-size: 16pt;"
+ "-fx-font-family: 'Courier New';"
+ "-fx-base: rgb(128, 0, 0);";

vBox.getChildren().addAll(imageView, button, text);
root.getChildren().add(vBox);
root.setStyle(style_root);
primaryStage.setScene(scene);
primaryStage.show();
}
}


JavaFX 2.0: Apply border on ImageView, using HBox with css style

JavaFX 2.0: Apply border on ImageView, using HBox with css style
package javafx_image;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @web http://mobile-web-app.blogspot.com/
*/
public class JavaFX_image extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com: Load image");
Group root = new Group();

double width = 400;
double height = 300;
Scene scene = new Scene(root, width, height, Color.WHITE);

Image image = new Image(getClass().getResourceAsStream("duke_44x80.png"));
ImageView imageView = new ImageView();
imageView.setImage(image);

//Inner border
HBox hBox_inner = new HBox();
String style_inner = "-fx-border-color: red;"
+ "-fx-border-width: 1;"
+ "-fx-border-style: dotted;";
hBox_inner.setStyle(style_inner);

//Outter border
HBox hBox_outter = new HBox();
String style_outter = "-fx-border-color: black;"
+ "-fx-border-width: 10;";
hBox_outter.setStyle(style_outter);

hBox_inner.getChildren().add(imageView);
hBox_outter.getChildren().add(hBox_inner);
root.getChildren().add(hBox_outter);
primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFX 2.0: Load image in ImageView

JavaFX 2.0: Load image in ImageView
package javafx_image;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://mobile-web-app.blogspot.com/
*/
public class JavaFX_image extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com: Load image");
Group root = new Group();

double width = 400;
double height = 300;
Scene scene = new Scene(root, width, height, Color.WHITE);

Image image = new Image(getClass().getResourceAsStream("duke_44x80.png"));
ImageView imageView = new ImageView();
imageView.setImage(image);

root.getChildren().add(imageView);
primaryStage.setScene(scene);
primaryStage.show();
}
}


Friday, February 10, 2012

JavaFX 2.0: StackPane

JavaFX 2.0: StackPane
package javafx_layoutpanes;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_LayoutPanes extends Application {

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
      launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
      primaryStage.setTitle("java-buddy.blogspot.com: StackPane");
      Group root = new Group();
    
      double width = 400;
      double height = 300;
    
      Scene scene = new Scene(root, width, height, Color.WHITE);

      StackPane stackPane = new StackPane();
      stackPane.setPrefSize(width, height);
      stackPane.setAlignment(Pos.CENTER);
    
      Button cbutton = new Button("Center Button");
      Button button = new Button("Button");
      Text text = new Text("Text");
    
      stackPane.getChildren().addAll(cbutton, button, text);
    
      root.getChildren().add(stackPane);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
}


Compare with Create StackPane using FXML.


JavaFX 2.0: HBox and VBox

The HBox layout pane arranging a series of nodes in a single row horizontally.
The VBox layout pane arranging a series of nodes in a single column vertically.

Example:
JavaFX 2.0: HBox and VBox
package javafx_layoutpanes;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_LayoutPanes extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com: HBox & VBox");
Group root = new Group();

double width = 400;
double height = 300;

Scene scene = new Scene(root, width, height, Color.WHITE);

BorderPane borderPane = new BorderPane();
borderPane.setPrefSize(width, height);

HBox hbox = new HBox();
hbox.getChildren().addAll(
new Button("Top 1"),
new Button("Top 2"),
new Button("Top 3"),
new Button("Top 4"),
new Button("Top 5"));

VBox vbox = new VBox();
vbox.getChildren().addAll(
new Button("Left A"),
new Button("left B"),
new Button("left C"),
new Button("left D"),
new Button("left E"));

borderPane.setTop(hbox);
borderPane.setBottom(new Button("Bottom"));
borderPane.setCenter(new Button("Center"));
borderPane.setLeft(vbox);
borderPane.setRight(new Button("Right"));

root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}

Thursday, February 9, 2012

JavaFX 2.0: BorderPane

JavaFX 2.0: BorderPane
package javafx_layoutpanes;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_LayoutPanes extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com: borderPane");
Group root = new Group();

double width = 400;
double height = 300;

Scene scene = new Scene(root, width, height, Color.WHITE);

BorderPane borderPane = new BorderPane();
borderPane.setPrefSize(width, height);

borderPane.setTop(new Button("Top"));
borderPane.setBottom(new Button("Bottom"));
borderPane.setCenter(new Button("Center"));
borderPane.setLeft(new Button("Left"));
borderPane.setRight(new Button("Right"));

root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}


Related: JavaFX 2.0: set width and height of Layout Pane - setPrefSize()

JavaFX 2.1 Developer Preview for Linux


The JavaFX 2.1 Developer Preview is now available for Linux, as well as Windows and Mac OS X.

Download link: http://www.oracle.com/technetwork/java/javafx/downloads/devpreview-1429449.html

Wednesday, February 8, 2012

JavaFX 2.0: RadioMenuItem and ToggleGroup

JavaFX 2.0: RadioMenuItem and ToggleGroup
package javafx_exmenu;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_exMenu extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

//Top Menu Bar
MenuBar menuBar = new MenuBar();

//Radio Menu
Menu radioMenu = new Menu("Radio Menu");

radioMenu.getItems().add(
RadioMenuItemBuilder.create()
.text("Radio Items 1")
.build());
radioMenu.getItems().add(
RadioMenuItemBuilder.create()
.text("Radio Items 2")
.selected(true)
.build());

//Toggle Group Menu
Menu toggleMenu = new Menu("Toggle Menu");
ToggleGroup toggleGroup = new ToggleGroup();
RadioMenuItem radioItemA = new RadioMenuItem("Option A");
RadioMenuItem radioItemB = new RadioMenuItem("Option B");
RadioMenuItem radioItemC = new RadioMenuItem("Option C");
radioItemA.setToggleGroup(toggleGroup);
radioItemB.setToggleGroup(toggleGroup);
radioItemC.setToggleGroup(toggleGroup);
toggleMenu.getItems().add(radioItemA);
toggleMenu.getItems().add(radioItemB);
toggleMenu.getItems().add(radioItemC);

menuBar.getMenus().add(radioMenu); //Add Radio Menu
menuBar.getMenus().add(toggleMenu); //Add Toggle Group Menu

menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
}

JavaFX 2.0: Ckeck Menu

Ckeck Menu is a MenuItem that can be toggled between selected and unselected states.
JavaFX 2.0: Ckeck Menu
package javafx_exmenu;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_exMenu extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

//Top Menu Bar
MenuBar menuBar = new MenuBar();

Menu menu1 = new Menu("Menu");

MenuItem menuItemA = new MenuItem("Item A");
menuItemA.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item A Clicked");
}
});

MenuItem menuItemB = new MenuItem("Item B");
menuItemB.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item B Clicked");
}
});

MenuItem menuItemC = new MenuItem("Item C");
menuItemC.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item C Clicked");
}
});

//Sub-Menu
Menu subMenu_s1 = new Menu("SubMenu 1");
MenuItem subMenuItem1 = new MenuItem("Sub Menu Item 1");
MenuItem subMenuItem2 = new MenuItem("Sub Menu Item 2");
MenuItem subMenuItem3 = new MenuItem("Sub Menu Item 3");
subMenu_s1.getItems().addAll(subMenuItem1, subMenuItem2, subMenuItem3);

//Check Menu
Menu checkMenu = new Menu("Check Menu");
checkMenu.getItems().add(
CheckMenuItemBuilder.create()
.text("Check Items 1")
.build());
checkMenu.getItems().add(
CheckMenuItemBuilder.create()
.text("Check Items 2")
.selected(true)
.build());


menu1.getItems().add(menuItemA);
menu1.getItems().add(menuItemB);
menu1.getItems().add(menuItemC);
menu1.getItems().add(subMenu_s1); //Add Sub-Menu

menuBar.getMenus().add(menu1);
menuBar.getMenus().add(checkMenu); //Add Check Menu

menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
}

Tuesday, February 7, 2012

JavaFX 2.0: Add submenu

JavaFX 2.0: Add submenu
package javafx_exmenu;

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.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_exMenu extends Application {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.WHITE);

//Top Menu Bar
MenuBar menuBar = new MenuBar();

Menu menu1 = new Menu("Menu");

MenuItem menuItemA = new MenuItem("Item A");
menuItemA.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item A Clicked");
}
});

MenuItem menuItemB = new MenuItem("Item B");
menuItemB.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item B Clicked");
}
});

MenuItem menuItemC = new MenuItem("Item C");
menuItemC.setOnAction(new EventHandler<ActionEvent>() {

@Override public void handle(ActionEvent e) {
System.out.println("Item C Clicked");
}
});

//Sub-Menu
Menu subMenu_s1 = new Menu("SubMenu 1");
MenuItem subMenuItem1 = new MenuItem("Sub Menu Item 1");
MenuItem subMenuItem2 = new MenuItem("Sub Menu Item 2");
MenuItem subMenuItem3 = new MenuItem("Sub Menu Item 3");
subMenu_s1.getItems().addAll(subMenuItem1, subMenuItem2, subMenuItem3);

menu1.getItems().add(menuItemA);
menu1.getItems().add(menuItemB);
menu1.getItems().add(menuItemC);
menu1.getItems().add(subMenu_s1); //Add Sub-Menu
menuBar.getMenus().add(menu1);

menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
root.getChildren().add(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
}