1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package myapp3;
  27 
  28 import java.util.Locale;
  29 import java.util.logging.Handler;
  30 import java.util.logging.LogRecord;
  31 import java.util.logging.Logger;
  32 import javafx.animation.KeyFrame;
  33 import javafx.animation.Timeline;
  34 import javafx.application.Application;
  35 import javafx.scene.Scene;
  36 import javafx.scene.control.TableColumn;
  37 import javafx.scene.control.TableView;
  38 import javafx.scene.control.cell.PropertyValueFactory;
  39 import javafx.scene.layout.StackPane;
  40 import javafx.stage.Stage;
  41 import javafx.util.Duration;
  42 import myapp3.pkg2.MyData;
  43 
  44 import static myapp3.Constants.*;
  45 
  46 /**
  47  * Modular test application for testing JavaFX beans.
  48  * This is launched by ModuleLauncherTest.
  49  */
  50 public class AppTableViewExported extends Application {
  51 
  52     /**
  53      * @param args the command line arguments
  54      */
  55     public static void main(String[] args) {
  56         try {
  57             Application.launch(args);
  58         } catch (Throwable t) {
  59             System.err.println("ERROR: caught unexpected exception: " + t);
  60             t.printStackTrace(System.err);
  61             System.exit(ERROR_UNEXPECTED_EXCEPTION);
  62         }
  63     }
  64 
  65     private Logger logger;
  66     private Handler logHandler;
  67 
  68     private void initLogger() {
  69         Locale.setDefault(Locale.US);
  70 
  71         // Initialize Logger
  72         logHandler = new Handler() {
  73             @Override
  74             public void publish(LogRecord record) {
  75                 final Throwable t = record.getThrown();
  76                 // detect any Throwable:
  77                 if (t != null) {
  78                     System.err.println("ERROR: unexpected exception was logged: " + record.getMessage());
  79                     t.printStackTrace();
  80                     System.exit(ERROR_UNEXPECTED_EXCEPTION);
  81                 }
  82             }
  83 
  84             @Override
  85             public void flush() {
  86             }
  87 
  88             @Override
  89             public void close() {
  90             }
  91         };
  92 
  93         logger = Logger.getLogger("javafx.scene.control");
  94         logger.addHandler(logHandler);
  95     }
  96 
  97     @Override
  98     public void start(Stage stage) throws Exception {
  99         initLogger();
 100 
 101         try {
 102             StackPane root = new StackPane();
 103             Scene scene = new Scene(root);
 104             TableView<MyData> tableView = new TableView<>();
 105 
 106             // Name column
 107             TableColumn<MyData, String> nameCol = new TableColumn<>();
 108             nameCol.setText("Name");
 109             nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
 110 
 111             // Value column
 112             TableColumn<MyData, Integer> valueCol = new TableColumn<>();
 113             valueCol.setText("Value");
 114             valueCol.setCellValueFactory(new PropertyValueFactory<>("value"));
 115 
 116             tableView.getColumns().addAll(nameCol, valueCol);
 117 
 118             tableView.getItems().add(new MyData("Row A", 1));
 119             tableView.getItems().add(new MyData("Row B", 2));
 120             tableView.getItems().add(new MyData("Row C", 3));
 121 
 122             root.getChildren().add(tableView);
 123 
 124             stage.setScene(scene);
 125             stage.show();
 126 
 127             // Hide the stage after the specified amount of time
 128             KeyFrame kf = new KeyFrame(Duration.millis(SHOWTIME), e -> stage.hide());
 129             Timeline timeline = new Timeline(kf);
 130             timeline.play();
 131         } catch (Error | Exception ex) {
 132             System.err.println("ERROR: caught unexpected exception: " + ex);
 133             ex.printStackTrace(System.err);
 134             System.exit(ERROR_UNEXPECTED_EXCEPTION);
 135         }
 136     }
 137 
 138     @Override public void stop() {
 139         System.exit(ERROR_NONE);
 140     }
 141 
 142 }