Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created April 22, 2013 00:49
Show Gist options
  • Save jewelsea/5431780 to your computer and use it in GitHub Desktop.
Save jewelsea/5431780 to your computer and use it in GitHub Desktop.
Determines the relative position (index in child list) of a node clicked in a JavaFX HBox.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
/** Determines the relative position (index in child list) of a node clicked in a HBox */
public class HBoxPositions extends Application {
private static final String[] ICON_LOCS =
{
"http://icons.iconarchive.com/icons/bevel-and-emboss/car/256/car-orange-icon.png",
"http://icons.iconarchive.com/icons/bevel-and-emboss/car/256/car-purple-icon.png",
"http://icons.iconarchive.com/icons/bevel-and-emboss/car/256/lorry-icon.png",
"http://icons.iconarchive.com/icons/bevel-and-emboss/car/256/van-bus-icon.png"
};
private Image[] icons = new Image[ICON_LOCS.length];
@Override public void init() {
int i = 0;
for (String loc: ICON_LOCS) {
icons[i] = new Image(loc);
i++;
}
}
@Override public void start(final Stage stage) {
final Label label = new Label();
label.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 20;");
final HBox images = new HBox(10);
for (Image icon: icons) {
final ImageView imageView = new ImageView(icon);
images.getChildren().add(imageView);
final DropShadow shadow = new DropShadow();
shadow.setColor(Color.FORESTGREEN);
final Glow glow = new Glow();
glow.setInput(shadow);
imageView.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
imageView.setEffect(glow);
}
});
imageView.setOnMouseExited(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
imageView.setEffect(null);
}
});
imageView.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
final Object selectedNode = mouseEvent.getSource();
final int selectedIdx = images.getChildren().indexOf(selectedNode);
label.setText(
"Selected Vehicle: " + (selectedIdx + 1)
);
}
});
}
final VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-padding: 10; -fx-background-color: cornsilk;");
layout.getChildren().addAll(images, label);
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) { launch(args); }
}
@jewelsea
Copy link
Author

Answer to StackOverflow question: Finding the pressed Node's position in a HBox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment