Javafx adding icons to menu and menuitem.




import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.MenuBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class MenuIcons extends Application
{
    public static void main(String[]args)
    {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage)
    {
        
        BorderPane bp = new BorderPane();
        Scene scene = new Scene(bp,600,400);
        
        MenuBar menuB = new MenuBar();//creating our MenuBar
        bp.setTop(menuB);//set our menubar at the top of the brderpane
        
        //creating icons to be used on our menu and menuitems
        Image downloadIcon = new Image(getClass().getResourceAsStream("download.png"));
        ImageView downloadView = new ImageView(downloadIcon);
        downloadView.setFitWidth(15);
        downloadView.setFitHeight(15);
        
        Image userIcon = new Image(getClass().getResourceAsStream("user.png"));
        ImageView userView = new ImageView(userIcon);
        userView.setFitWidth(15);
        userView.setFitHeight(15);
        
       
        
        Image uploadsIcon = new Image(getClass().getResourceAsStream("upload.png"));
        ImageView uploadView = new ImageView(uploadsIcon);
        uploadView.setFitWidth(15);
        uploadView.setFitHeight(15);
        
        Image cameraIcon = new Image(getClass().getResourceAsStream("camera.png"));
        ImageView cameraView = new ImageView(cameraIcon);
        cameraView.setFitWidth(15);
        cameraView.setFitHeight(15);
        
        Image EditIcon = new Image(getClass().getResourceAsStream("edit.png"));
        ImageView editView = new ImageView(downloadIcon);
        editView.setFitWidth(15);
        editView.setFitHeight(15);
        
        //creating the menu and menuitems
        Menu userMenu = new Menu("user");
        userMenu.setGraphic(userView);//setting the user icon to menu user
        //creating the download menuitem
        MenuItem downloadItem = new MenuItem("downloads");
        downloadItem.setGraphic(downloadView);//setting the download icon to download menuitem
        //creating the upload menuitem
        MenuItem uploadItem = new MenuItem("uploads");
        uploadItem.setGraphic(uploadView);//setting the upload icon to upload menuitem
        //creating the photos menuitem
        MenuItem cameraItem = new MenuItem("photos");
        cameraItem.setGraphic(cameraView);//setting the cameraIcon to cameraItem
        //creating the edit menuitem
        MenuItem editItem = new MenuItem("edit");
        editItem.setGraphic(editView);//setting the editIcon to edit menuitem
        
        userMenu.getItems().addAll(downloadItem,uploadItem,cameraItem,editItem);
        
        
        menuB.getMenus().add(userMenu);//adding menu to menubar
        
        primaryStage.setTitle("Iconified Menu and MenuItems");
        primaryStage.setScene(scene);
        primaryStage.show();
       
    }

}
The code above will give the result above.

add your icons in the source folder of your project.

Comments

Post a Comment

Popular posts from this blog

Javafx inserting uploaded image into database.

Javafx displaying image from database.