JavaFX:如何在gridpane中获取列和行索引? [英] JavaFX : How to get column and row index in gridpane?
问题描述
当我点击时,我试图获取gridpane的行/列索引。
I tried to get the row/column index of gridpane, when I click.
我正在寻找像 getSelectedColumn()(java swing)
I am looking for method like as getSelectedColumn() in JTable(java swing)
我搜索了这个。 gridPane.getRowIndex(node)
但这对我没有帮助。因为它需要 节点 名称来获取行/列索引。
But it doesn't help to me. Because it need node name to get row/column index.
我为语法创建了一个网格组件。所以节点名称是相同的。
我如何获得价值?我的部分代码如下。
I make a component of grid by for grammar. So the node name is same. How can I get the value? My partial code is below.
for (int i = 0; i < imageName.length; i++) {
try {
img = new Image(getClass().getResourceAsStream(imageName[i]));
} catch (Exception e) {
System.out.println("exception : " + e);
}
ImageView imageview = new ImageView(img);
imageview.setFitWidth(40);
imageview.setFitHeight(40);
HBox hbox= new HBox();
hbox.getChildren().add(imageview);
tile1.setHgap(40);
tile1.setVgap(40);
tile1.add(hbox, i+2, 0);
}
推荐答案
这是一个关于你如何的例子如果您只想在网格窗格上使用鼠标监听器而不在单元格中的节点上,则可以执行此操作。为简单起见,我使用Label作为单元节点,但您可以使用您喜欢的任何内容。
Here's an example about how you could do it if you want only a mouse listener on the gridpane and not on the nodes in the cells. For simplicity I used a Label as cell node, but you can use whatever you prefer.
public class Demo extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(final Stage primaryStage) {
Pane root = new Pane();
GridPane gridPane = new GridPane();
gridPane.setGridLinesVisible(true);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
Label label = new Label("Label " + i + "/" + j);
label.setMouseTransparent(true);
GridPane.setRowIndex(label, i);
GridPane.setColumnIndex(label, j);
gridPane.getChildren().add(label);
}
}
root.getChildren().add( gridPane);
Scene scene = new Scene(root, 400, 300, Color.WHITE);
primaryStage.setScene(scene);
primaryStage.show();
gridPane.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
for( Node node: gridPane.getChildren()) {
if( node instanceof Label) {
if( node.getBoundsInParent().contains(e.getSceneX(), e.getSceneY())) {
System.out.println( "Node: " + node + " at " + GridPane.getRowIndex( node) + "/" + GridPane.getColumnIndex( node));
}
}
}
}
});
}
}
你看到了在控制台中单击了单元格信息。
You see the clicked cell information in the console.
如果你将监听器放在单元节点而不是gridpane上,那么同样的方法也可行,这里作为lambda表达式:
The same would work if you'd put the listener on the cell node instead of the gridpane, here as a lambda expression:
label.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> System.out.println( "Node: " + label + " at " + GridPane.getRowIndex( label) + "/" + GridPane.getColumnIndex( label)));
但要注意 getRowIndex 和 getColumnIndex 方法仅在数据先前已设置时有效,如这些方法的文档中所述。
But be aware that the getRowIndex and getColumnIndex methods work only if the data were previously set, as specified in the documentation to these methods.
我没有关于你打算实现什么的信息,但我个人更喜欢使用节点本身而不是布局管理器中可能会改变的一些索引。
I have no information about what you intend to achieve, but personally I prefer to work with the nodes themselves instead of some indices in a layout manager which may change.
这篇关于JavaFX:如何在gridpane中获取列和行索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!