Я добавил 3 метки в сетку и добавил обработчик щелчка на каждую из меток. Смотрите, хотите ли вы этого.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Control;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GridPaneStyle extends Application
{
@Override
public void start(final Stage stage)
{
// create a grid with some sample data.
GridPane grid = new GridPane();
final Label l1 = new Label("1");
final Label l2 = new Label("2");
final Label l3 = new Label("3");
l1.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent arg0)
{
l1.setStyle("-fx-background-color:yellow;");
}
});
l2.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent arg0)
{
l2.setStyle("-fx-background-color:yellow;");
}
});
l3.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent arg0)
{
l3.setStyle("-fx-background-color:yellow;");
}
});
grid.addRow(0, l1, l2, l3);
for (Node n : grid.getChildren())
{
Control control = (Control) n;
control.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
control.setStyle("-fx-background-color: tomato; -fx-alignment: center;");
}
grid.setStyle("-fx-background-color: palegreen; -fx-padding: 2; -fx-hgap: 2; -fx-vgap: 2;");
grid.setSnapToPixel(false);
ColumnConstraints oneThird = new ColumnConstraints();
oneThird.setPercentWidth(100/3.0);
oneThird.setHalignment(HPos.CENTER);
grid.getColumnConstraints().addAll(oneThird, oneThird, oneThird);
RowConstraints oneHalf = new RowConstraints();
oneHalf.setPercentHeight(100/2.0);
oneHalf.setValignment(VPos.CENTER);
grid.getRowConstraints().addAll(oneHalf, oneHalf);
StackPane layout = new StackPane();
layout.setStyle("-fx-background-color: white;");
layout.getChildren().addAll(grid);
stage.setScene(new Scene(layout, 600, 400));
stage.show();
}
public static void main(String[] args)
{
launch();
}
}
Вы хотите выделить ячейку, когда событие перетаскивания происходит на ячейке «GridPane»? – Aspirant
Я хочу щелкнуть мышью, а когда ее отпустить, выбранная ячейка даст некоторый цвет – user2858883
Как вы собираетесь * дать цвет *? Выбирается * подборщик цветов *? – Aspirant