Reputation: 1
I have a GridPane in FXML document with id of matrix. And I want to add labels to each cell in the matrix. Here is my controller:
package application.view;
import java.awt.Button;
import java.awt.Insets;
import java.awt.Label;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;
public class viewController {
@FXML
private GridPane matrix;
private Label[][] label = new Label[10][10];
public viewController() {
}
@FXML
private void initialize() {
}
@FXML
private void setMatrix() {
for (int i = 0; i < label.length; i++) {
for (int j = 0; j < label[i].length; j++) {
label[i][j].setText("1");
matrix.add(label[i][j], i, j);
}
}
}
}
Here is my FXML Document:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.view.viewController">
<children>
<SplitPane dividerPositions="0.8467336683417085" orientation="VERTICAL" prefHeight="200.0" prefWidth="160.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<GridPane fx:id="matrix" alignment="CENTER" disable="true" gridLinesVisible="true" layoutX="73.0" layoutY="125.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<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 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 hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<HBox alignment="CENTER" prefHeight="65.0" prefWidth="200.0" spacing="10.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Button fx:id="refresh" mnemonicParsing="false" text="Refresh" />
<Button fx:id="findBlock" mnemonicParsing="false" text="Find Largest Block" />
</children>
</HBox>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
As you can see I get the GridPane of id matrix. There I want to add a label in each cell. The label will obviously have a number in it.The only thing that is giving me an error is the matrix.add()
. It says can't convert to a node. Please help. Thank you!
Upvotes: 0
Views: 8499
Reputation: 316
Like user4132613 said, import javafx.scene.control.Label
instead of java.awt.Label
. Actually, never use AWT classes with JavaFX.
And you've created the array of labels, but you haven't instantiated the objects inside the array. Method setMatrix() should be:
@FXML
private void setMatrix() {
for (int i = 0; i < label.length; i++) {
for (int j = 0; j < label[i].length; j++) {
label[i][j] = new Label(); // This is missing in the original code
label[i][j].setText("1");
matrix.add(label[i][j], i, j);
}
}
}
Upvotes: 3