2018년 9월 4일 화요일

[JavaFX] JavaFX Programming without FXML


1. Main Application

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
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.io.File;
import java.util.List;

import demo.FxTableViewDemo;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ToolBar;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;


public class AppMain extends Application {
    
    Stage mainStage;
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            mainStage = primaryStage;
            primaryStage.setTitle("Hello World!");
            primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/res/Window32.png")));
            
            VBox vbox = FxContainer.getVBox(0,0,0,0,0);
            vbox.getChildren().add(makeMenuBar());
            vbox.getChildren().add(makeToolBar());

            BorderPane pane = FxContainer.getBorderPane(0,0,0,0);
            pane.setTop(vbox);
            pane.setCenter(makeSimpleTableView());
            pane.setBottom(makeStatusBar());

            Scene scene = new Scene(pane, 600, 400);
            primaryStage.setScene(scene);
            primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
                public void handle(WindowEvent we) {
                    System.exit(0);
                }
            });             
            primaryStage.show();            

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    Runnable runnableExit = new Runnable() {
        @Override
        public void run() {
            boolean result = FxDialog.yesno(mainStage, "Exit", "Do you want exit progrma ?");
            if( result ) {
                System.exit(0);
            }
        }
    };
    
    Runnable runnableAboutMenu = new Runnable() {
        @Override
        public void run() {
            FxDialog.alert(mainStage, "About", "JavaFX Example V1.0");
        }
    };
    
    Runnable runnableAboutToolbar = new Runnable() {
        @Override
        public void run() {
            FxDialog.alert("/res/About32.png", "About", "JavaFX Example V1.0");
        }
    };
    
    public MenuBar makeMenuBar() {
        FxMenu menu = new FxMenu();
        menu.addMenu("File");
        menu.addMenuItem("Exit", "/res/Exit16.png", runnableExit);
        menu.addMenu("Help");
        menu.addMenuItem("About", "/res/About32.png", runnableAboutMenu);
        return menu.getMenuBar();
    }
    
    public ToolBar makeToolBar() {
        FxToolBar toolbar = new FxToolBar();
        toolbar.addToolBarItem(null, "/res/Exit32.png", runnableExit);
        toolbar.addToolBarItem(null, "/res/About32.png", runnableAboutToolbar);     
        return toolbar.getToolBar();
    }
    
    public HBox makeStatusBar() {
        FxStatusBar statusbar = new FxStatusBar("Ready");
        return statusbar.getHBox();
    }
    
    public StackPane makeSimpleTableView() {
        FxTable table = new FxTable();
        
        table.addColumns( new String[] { "Name", "Size" } );
        table.addColumn("Status");
        table.setColumnAlign( new Integer[] { -1, 1, 0 } );
        table.setColumnWidthFixed( new Integer[] { 400, 160, 40 } ); //1/2, 1/4, 1/4 -> 1/1

        table.addCell( new String[] { "AAA", String.valueOf(1234), "X" });
        table.addCell( new String[] { "BBB", String.valueOf(5678), "X" });
        table.addCell( new String[] { "CCC", String.valueOf(12345678), "X" });
        
        FxContextMenu menu = new FxContextMenu();
        menu.addMenuItem("Delete", (ActionEvent event) -> {
            FxTableRow item = table.table.getSelectionModel().getSelectedItem();
            System.out.println("Selected item: " + item.getValue(0));
            Number itemIndex = table.table.getSelectionModel().getSelectedIndex();
            table.table.getItems().remove(itemIndex.intValue());
        });
        
        table.tableSetContextMenu(menu.getMenu());
        
        table.setDragAndDropHandler(new FxTableEventHandler() {
            @SuppressWarnings("unchecked")
            @Override public void run(Object object) {
                List<File> files = (List<File>) object;
                if( files != null && files.size() > 0 ) {
                    for( File f : files ) {
                        println(f.getPath());
                        table.addCell(new String[] { f.getPath(), String.valueOf(f.length()), "X" } );
                    }
                }
            }
        });
        table.tableSetItemHandler(new FxTableEventHandler() {
            @Override public void run(Object object) {
                FxTableRow[] rows = (FxTableRow[]) object;
                if( rows.length > 1 ) {
                    rows[1].setValue(2, "O");
                }
            }
        });
        StackPane pane = new StackPane();
        pane.getChildren().addAll(table.getTable());
        return pane;
    }
    
    public static void println(String s) {
        System.out.println(s);
    }
    
    public StackPane makeTableView() {
        FxTableViewDemo table = new FxTableViewDemo();
        table.addCell("AAA", 111);
        table.addCell("BBB", 222);
        table.addCell("CCC", 333);
        StackPane pane = new StackPane();
        pane.getChildren().addAll(table.getTable());
        return pane;
    }
    
}


Output:






댓글 없음:

댓글 쓰기