2016-10-04 4 views
0

私はMenuBarToolBarの両方のアプリケーションを持っています。 ControlsFXのドキュメントでは、別のクラスのアクションイベントロジックを定義し、fxmlで定義されたボタン、menuitems、およびtogglebuttonsに割り当てることが可能です。多かれ少なかれ、PHPフレームワーク(例えばラーベル)のルータに似ています。ここControlFXアクション使用法

は説明

はJavaFXの内のアクションは、コントロールからの機能及び状態を分離するために使用することが可能です。たとえば、同じ機能を実行する2つ以上のコントロール(メニューやツールバーのコントロールなど)がある場合は、Actionオブジェクトを使用してその機能を実装することを検討してください。 Actionオブジェクトは、ボタン、メニューアイテムなどのアクションイベント発生コンポーネントの状態の集中処理を提供します。アクションで処理できる状態には、テキスト、グラフィック、テキスト(ツールヒントのテキスト)、無効などがあります。

問題は、アプリケーションで使用するのに十分な情報を取得できなかったことです。ここで私が持っている簡単な例は、私の問題は、メソッドを割り当てる方法であるAppRouter

public class AppRouter { 
public AppRouter(){ 
    ActionMap.register(this); 
} 

public void testOne(){ 
    System.out.println("testOne"); 
} 

public void testTwo(){ 
    System.out.println("testTwo"); 
} 

public void testThree(){ 
    System.out.println("testThree"); 
} 
} 

これまで

public class RootController implements Initializable { 
@FXML 
private MenuItem menuOne; 
@FXML 
private MenuItem menuTwo; 
@FXML 
private MenuItem menuThree; 
@FXML 
private Button tbOne; 
@FXML 
private Button tbTwo; 
@FXML 
private Button tbThree; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
} 
} 

root.fxml

<?xml version="1.0" encoding="UTF-8"?> 
<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Menu?> 
<?import javafx.scene.control.MenuBar?> 
<?import javafx.scene.control.MenuItem?> 
<?import javafx.scene.control.ToolBar?> 
<?import javafx.scene.layout.BorderPane?> 
<?import javafx.scene.layout.VBox?> 
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"> 
    <top> 
     <VBox BorderPane.alignment="CENTER"> 
     <children> 
      <MenuBar> 
      <menus> 
      <Menu mnemonicParsing="false" text="File"> 
       <items> 
       <MenuItem fx:id="menuOne" mnemonicParsing="false" text="One" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Edit"> 
       <items> 
       <MenuItem fx:id="menuTwo" mnemonicParsing="false" text="Two" /> 
       </items> 
      </Menu> 
      <Menu mnemonicParsing="false" text="Whatever"> 
       <items> 
       <MenuItem fx:id="menuThree" mnemonicParsing="false" text="Three" /> 
       </items> 
      </Menu> 
      </menus> 
     </MenuBar> 
     <ToolBar prefHeight="40.0" prefWidth="200.0"> 
      <items> 
      <Button fx:id="tbOne" mnemonicParsing="false" text="One" /> 
       <Button fx:id="tbTwo" layoutX="10.0" layoutY="13.0" mnemonicParsing="false" text="Two" /> 
       <Button fx:id="tbThree" layoutX="66.0" layoutY="13.0" mnemonicParsing="false" text="Three" /> 
      </items> 
     </ToolBar> 
    </children> 
    </VBox> 
    </top> 
</BorderPane> 

メイン

public class MainApp extends Application { 
public static void main(String[] args) throws Exception {launch(args); } 

public void start(Stage stage) throws Exception { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/root.fxml")); 
    loader.setController(new RootController()); 
    Scene scene = new Scene((Parent)loader.load(), 400, 200); 
    stage.setTitle("ControlFX Action API"); 
    stage.setScene(scene); 
    stage.show(); 
} 
} 

ですボタンへのAppRouterのそしてRootController

更新

でメニューアイテムは、私は喜んで、あまりにも他の任意の代替の答えを受け入れます。

+0

インスタンスのAppRouterクラスはどこに作成されますか? – GOXR3PLUS

答えて

0

のは、あなたが にApplicationクラスAppRouterクラスのインスタンスを作成することを想定してみましょう:次に、あなたが FXMLControllerで、AppRouterを受け取るメソッドを持つことができます

public class MainApp extends Application { 

AppRouter appRouter = new AppRouter(); //here................ 

public static void main(String[] args) throws Exception {launch(args); } 

public void start(Stage stage) throws Exception { 
    FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/root.fxml")); 
    loader.setController(new RootController()); 
    Scene scene = new Scene((Parent)loader.load(), 400, 200); 
    stage.setTitle("ControlFX Action API"); 
    stage.setScene(scene); 
    stage.show(); 
} 
} 

を:

public class RootController implements Initializable { 
@FXML 
private MenuItem menuOne; 
@FXML 
private MenuItem menuTwo; 
@FXML 
private MenuItem menuThree; 
@FXML 
private Button tbOne; 
@FXML 
private Button tbTwo; 
@FXML 
private Button tbThree; 

@Override 
public void initialize(URL url, ResourceBundle rb) { 
    // TODO 
} 

public void acceptRouter(AppRouter router){ 


    //register or call router methods here 
    } 
} 

今作成したコントローラをMainAppクラスに追加するにはどうすればよいですか?

FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/root.fxml")); 
    loader.setController(new RootController()); 

    RootController controller = loader.getController(); //simple as this,although check the method name if it is the same cause i added this from phone 
    controller.acceptRouter(appRouter); 
関連する問題