JavaFX/controller/HeaderMenu.java

82 lines
1.6 KiB
Java

package controller;
import java.util.ArrayList;
import javafx.event.ActionEvent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import model.HeaderMenuItem;
public class HeaderMenu{
/* Data */
private ArrayList<HeaderMenuItem> items;
/* Constructor */
public HeaderMenu(){
this.items = new ArrayList<HeaderMenuItem>();
}
public void addItem(HeaderMenuItem p_item) {
this.items.add(p_item);
}
/* Content Builder */
public void render(FlowPane p_parent) {
/* (1) Initialize variables */
int i, il;
/* (2) Create each item */
for( i = 0, il = this.items.size() ; i < il ; i++ ) {
// Create ImageView
ImageView v_view = new ImageView();
// Get image
Image v_img = new Image( this.items.get(i).getImageURI() );
// Set image on button
v_view.setImage(v_img);
// Set default props.
v_view.setId("header_menu_item_"+this.items.get(i).getName());
v_view.getStyleClass().add("header_menu_item");
// HOVER
v_view.addEventHandler(MouseEvent.MOUSE_MOVED, e -> {
System.out.println("hover");
ImageView view = (ImageView) e.getTarget();
System.out.println( view.getId() );
e.consume();
});
// CLICK
v_view.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> {
System.out.println("clicked");
ImageView view = (ImageView) e.getTarget();
e.consume();
});
// Add to parent
p_parent.getChildren().add(v_view);
}
}
public void onClick(ActionEvent p_event){
System.out.println("You clicked the button");
}
}