2018년 9월 9일 일요일

[JavaFx] MenuBar Example


1. MenuBar

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.image.Image;
import javafx.scene.image.ImageView;

public class FxMenu {

    MenuBar menuBar;
    Menu currMenu;
    
    public FxMenu() {
        menuBar = new MenuBar();
    }
    
    public MenuBar getMenuBar() {
        return menuBar;
    }
    
    public void addMenu(String title) {
        currMenu = new Menu(title);
        menuBar.getMenus().add(currMenu);
    }

    public MenuItem addMenuItem(String title, String iconFile, Runnable runnable) {
        MenuItem item = new MenuItem( title);
        if( iconFile != null ) {
            item.setGraphic(new ImageView(new Image(getClass().getResourceAsStream(iconFile))));
        }
        item.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent t) {
                runnable.run();
            }
        });
        currMenu.getItems().add(item);
        return item;
    }       
    
    public MenuItem addMenuItem(String title, Runnable runnable) {
        return addMenuItem(title, null, runnable);
    }
}


댓글 없음:

댓글 쓰기