2017-07-21 7 views
-1

によってクリックで情報を取得する方法..私はJavaFXのは - 私はこれは私がこれについて開く最後のポストであると思いますマウス

これは...整数nullではなく、常に返しGridPaneの私のCOORDが、すべてを試してみましたこのようなスレッドではなく、特定の質問ですので、終了しないでください、または他の操作をしてください。私はこの問題を解決しなければならない。

私の実態: Project othello Scene Builder は、ここに私のコードです。私の質問は..なぜmyGrid(GridPane)をクリックするといつも 'null'を返しますか?ここで

My method clickGrid .. FXMLで私のコントローラありがとう:

Controller's name

+1

関連する方法の画像とポストコードスニペットを取り出してください。画像は一度に消えてしまい、読みづらいことがあります。 –

+2

コードをイメージとしてのテキストとして掲載しないでください。コードに提供されない画像には情報はありません...さらに、コントローラフィールドとfxmlファイルの関連部分(コード)を含む部分を投稿してください... – fabian

+2

これは[以前投稿した質問](https://stackoverflow.com/q/45219540/2189127)。イベントハンドラが別のノードに登録されていますか、それともまだグリッドペインに登録されていますか? –

答えて

0

をあなたのGridPaneNodeのいくつかのタイプを追加していますか?このサンプルアプリケーションでは、それぞれGridStackPaneが追加されています。同じイベントハンドラがすべてStackPaneに登録されています。

メイン

package javafxapplication147; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

/** 
* 
* @author blj0011 
*/ 
public class JavaFXApplication147 extends Application 
{ 

    @Override 
    public void start(Stage stage) throws Exception 
    { 
     Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); 

     Scene scene = new Scene(root); 

     stage.setScene(scene); 
     stage.show(); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) 
    { 
     launch(args); 
    } 

} 

コントローラ

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.Node; 
import javafx.scene.control.Label; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.GridPane; 

/** 
* 
* @author blj0011 
*/ 
public class FXMLDocumentController implements Initializable 
{ 

    @FXML 
    private Label lblMain; 

    @FXML 
    private void handleOnMouseClicked(MouseEvent event) 
    { 
     Node source = (Node)event.getSource(); 
     Integer colIndex = (GridPane.getColumnIndex(source) == null) ? 0 : (GridPane.getColumnIndex(source)); 
     Integer colRow = (GridPane.getRowIndex(source) == null) ? 0 : (GridPane.getRowIndex(source)); 
     lblMain.setText(colIndex + " : " + colRow); 
    } 

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

} 

FXML

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.geometry.Insets?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.RowConstraints?> 
<?import javafx.scene.layout.StackPane?> 
<?import javafx.scene.layout.VBox?> 

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication147.FXMLDocumentController"> 
    <children> 
     <VBox layoutX="48.0" layoutY="14.0" prefHeight="200.0" prefWidth="100.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="50.0" AnchorPane.rightAnchor="50.0" AnchorPane.topAnchor="0.0"> 
     <children> 
      <GridPane gridLinesVisible="true" style="-fx-background-color: green;"> 
       <columnConstraints> 
       <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
        <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
       </columnConstraints> 
       <rowConstraints> 
        <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" /> 
       <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" /> 
       <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" /> 
       <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" /> 
        <RowConstraints minHeight="100.0" prefHeight="100.0" vgrow="SOMETIMES" /> 
       </rowConstraints> 
       <VBox.margin> 
        <Insets top="10.0" /> 
       </VBox.margin> 
       <children> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="1" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="1" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="2" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="2" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="2" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="3" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="3" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="3" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="3" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.rowIndex="4" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="4" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="4" /> 
        <StackPane onMouseClicked="#handleOnMouseClicked" prefHeight="150.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="4" /> 
       </children> 
      </GridPane> 
      <StackPane prefHeight="150.0" prefWidth="200.0"> 
       <children> 
        <Label fx:id="lblMain" text="Label" /> 
       </children> 
      </StackPane> 
     </children> 
     </VBox> 
    </children> 
</AnchorPane> 
+0

うまく動作します!あなたのファイルFXMLを見て、私は理解しました!ありがとう – Alberto32

関連する問題