2017-06-26 7 views
0

私の質問は、プログラムごとに複数のユーザーインターフェイス画面を表示する方法です。私はこの質問が以前に尋ねられたと確信していますが、私は私のために働く解決策を見つけられませんでした。私が話しているシナリオについては、エキゾチックなものはありません。最初は、画面からの入力を検証し、エラーの場合には同じ画面を再表示するだけです。同じプログラムに2つのjavafx GUI画面を表示するには

私は、第2のより複雑なシナリオの観点から、入力データ画面の表示、入力の処理、出力を表示します。これは、最初の5つのテキストボックスとコマンドボタンを持つ単純な画面ではFXMLファイルが使用され、2つ目は複数選択のリストボックスではないという事実が多少複雑になりました。フローは次のとおり

1.メインプログラムはFXMLと何らかの形でまたは別のコール

3.入力を受信し、出力を生成するためにそれらを処理するコントローラをロードするローダプログラム

2を呼び出します。

最終ステップは、複数選択リストボックスの形式で出力を表示することです。第1のGUIは、入力を処理するための別個のファイルであるコントローラを使用し、第2のGUIは、画面定義と同じファイル内にあるイベントハンドラを使用して、ユーザがコマンドボタン。

様々なSOポストが移動するための方法は、最初のGUIが完了した後を介してアプリケーションをシャットダウンしますが

Platform.setImplicitExit(偽)と、バックグラウンドで行くのJavaFXランタイムを維持しないことであると述べています。

各GUIを定義して、表示したいシーンにシーンを切り替えるだけです。しかし、ここで私が説明したシナリオでは、コードを記述していますか? 2番目のGUIには、画面定義、イベントハンドラ、およびシーン切り替えコードという3つの部分があります。それぞれどこに入れますか? #2または#3。あなたが#2にいくつかを入れ、#3にいくつかを入れたら、#3はあなたが#2で行ったことをどのように知っていますか?

#2 FMXLローダーのためのコード:別途ランニングうまく働いた#3の

public class inputData extends Application { 
    public static void load() {  
     launch(); 
    } 
    @Override 
    public void start(Stage stage) throws Exception { 

     GridPane inpRoot = FXMLLoader.load(getClass().getResource("inputData.fxml")); 
     Scene inpScene = new Scene(inpRoot, 300, 275); 

     stage.setTitle("Amsnag 2.1 - Query Input"); 
     stage.setScene(inpScene); 
     stage.show(); 
    } 
} 

コード、リストボックスの定義とハンドラ、。それは、それが失敗したプログラムの残りの部分にそれを組み込もうとしたときだけです。

public class multiList extends Application { 
    public static void load() {  
     launch(); 
    } 
    public static final ObservableList options = FXCollections.observableArrayList(); 

    @Override 
     public void start(final Stage stage) {  
     final ListView<String> listView = new ListView<>();   
     listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 
// load list from DB 
     Connection conn = sql.connect(); 
     try { 
    // initialize option table 
      ResultSet rs = sql.select(conn, 
       "select distinct connDesc,accom from option order by connDEsc,accom"); 
      while (rs.next()) { 
       String opt = rs.getString("connDesc") + ": " + rs.getString("accom"); 
       listView.getItems().add(opt); 
      }                  
      conn.close(); 
     } 
     catch (SQLException e) { 
      System.out.println(e.getMessage()+ " from init"); 
     } 
// button to display fares 
     final Button displayButton = new Button("Display Fares"); 
// handle button click 
     displayButton.setOnAction(new EventHandler<ActionEvent>() { 
     @Override public void handle(ActionEvent event) { 
      Platform.exit(); // close list box 
      ObservableList selectedIndices = listView.getSelectionModel().getSelectedItems(); 
// lcreate temp table with selected options 
      Connection conn = sql.connect(); 
      try { 
// initialize option table 
       ResultSet rs = sql.select(conn, 
        "create temporary table selected (connDesc varchar(200),accom varchar(50))"); 
       for(Object o : selectedIndices){ 
        String option = o.toString(); 
// extract connDesc+accom from displayed option 
        msg.g(option); 
       }   
       conn.close(); 
      } 
      catch (SQLException e) { 
       System.out.println(e.getMessage()+ " from init"); 
      }   
     } 
    } ); // end of display handler 
// quit button 
    final Button resetButton = new Button("Quit"); 
    resetButton.setOnAction(new EventHandler<ActionEvent>() { 
    @Override 
     public void handle(ActionEvent event) { 
      Platform.exit(); 
     } 
    }); 
    final HBox controls = new HBox(10); 
    controls.setAlignment(Pos.CENTER); 
    controls.getChildren().addAll(displayButton, resetButton); 

    final VBox layout = new VBox(10); 
    layout.setAlignment(Pos.CENTER); 
    layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;"); 
    layout.getChildren().setAll(listView, controls); 
    layout.setPrefWidth(320);``enter code here 

    Scene scene = new Scene(layout); 
// stage.setScene(new Scene(layout)); 
    stage.setScene(scene); 
    stage.setTitle("Select one or more options"); 
    stage.show(); 
    } 
    public static void main(String[] args) { launch(args); } 
} 
+1

あなたのアプリケーションには1つの 'Application'サブクラスがあります。 2つのクラスで何をしようとしているのかははっきりしていません。おそらくhttps://stackoverflow.com/questions/32464698/java-how-do-i-start-a-standalone-application-from-the-current-one-when-both-areが役立ちますか? –

+0

私は、2番目のコードセットであるmultiListクラスが動作するスタンドアロンバージョンであることを明確にしていませんでした。私がしようとしているのは、2つのGUIを表示することです。 –

+0

他のアプリケーションで 'Application'サブクラスを再利用することはできません。別のクラスに再利用したい部分をファクターにします。これは私が上にリンクした質問と同じです。 –

答えて

0

Applicationサブクラスを別のアプリケーションで再利用することはできません。

Applicationクラスは、アプリケーション全体、またはより具体的にはそのライフサイクルを表します。そのため、アプリケーションのライフサイクルの適切な時点でFX Application Toolkitによって呼び出されるinit(),start()、およびstop()などのメソッドがあります。

あなたmultiList(脇:proper naming conventionsを使用してください)のレイアウトクラスはstart()方法で行われ、それが唯一のアプリケーションの開始時に発生する可能性があります。ここにレイアウトコードを置くことで、別のアプリケーションの後の時点で実行されるように再利用することができなくなります。

だから、別のクラスにMultiListのレイアウトを移動:

public class MultiList { 

    public static final ObservableList options = FXCollections.observableArrayList(); 

    private final VBox view ; 

    public MultiList() {  
     final ListView<String> listView = new ListView<>();   
     listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE); 
     // load list from DB 
     Connection conn = sql.connect(); 
     try { 
      // initialize option table 
      ResultSet rs = sql.select(conn, 
       "select distinct connDesc,accom from option order by connDEsc,accom"); 
      while (rs.next()) { 
       String opt = rs.getString("connDesc") + ": " + rs.getString("accom"); 
       listView.getItems().add(opt); 
      }                  
      conn.close(); 
     } 
     catch (SQLException e) { 
      System.out.println(e.getMessage()+ " from init"); 
     } 
     // button to display fares 
     final Button displayButton = new Button("Display Fares"); 
      // handle button click 
      displayButton.setOnAction(new EventHandler<ActionEvent>() { 
      @Override public void handle(ActionEvent event) { 
       Platform.exit(); // close list box 
       ObservableList selectedIndices = listView.getSelectionModel().getSelectedItems(); 
       // create temp table with selected options 
       Connection conn = sql.connect(); 
       try { 
        // initialize option table 
        ResultSet rs = sql.select(conn, 
         "create temporary table selected (connDesc varchar(200),accom varchar(50))"); 
        for(Object o : selectedIndices){ 
         String option = o.toString(); 
    // extract connDesc+accom from displayed option 
         msg.g(option); 
        }   
        conn.close(); 
       } catch (SQLException e) { 
        System.out.println(e.getMessage()+ " from init"); 
       }   
      } 
     }); // end of display handler 
     // quit button 
     final Button resetButton = new Button("Quit"); 
     resetButton.setOnAction(new EventHandler<ActionEvent>() { 
     @Override 
      public void handle(ActionEvent event) { 
       Platform.exit(); 
      } 
     }); 
     final HBox controls = new HBox(10); 
     controls.setAlignment(Pos.CENTER); 
     controls.getChildren().addAll(displayButton, resetButton); 

     view = new VBox(10); 
     view.setAlignment(Pos.CENTER); 
     view.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;"); 
     view.getChildren().setAll(listView, controls); 
     view.setPrefWidth(320); 


    } 

    public Parent getView() { 
     return view ; 
    } 

} 

あなたは自分自身でこれをテストする場合さて、あなたはそれのためのアプリケーションを書くことができます。

public class MultiListApp extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     MultiList multiList = new MultiList() ; 
     Scene scene = new Scene(multiList.getView()); 
     primaryStage.setScene(scene); 
     primarStage.setTitle("Select one or more options"); 
     primaryStage.show(); 
    } 
} 

InputData.fxmlのコントローラクラスでは、同じことをすることができます:

public class InputDataController { 

    @FXML 
    private void someEventHandler() { 
     MultiList multiList = new MultiList() ; 
     Scene scene = new Scene(multiList.getView()); 
     Stage stage = new Stage(); 
     stage.setScene(scene); 
     stage.setTitle("Select one or more options"); 
     stage.show(); 
    } 
} 
+0

私は応答を研究しています。皆さんありがとう –

関連する問題