'Find controls that are near a particular control

I want to know, Can I get the location of other control from a particular control? Tried to search on the internet and found C# Get a control's position on a form but didn't understand it properly. Here's an example:

Form with a four-button grid

As you can see I have used 4 buttons, Now I want to know Which control is Left/Right/Up/Down to Button 3.



Solution 1:[1]

It's not possible on the server side to determine how "near", in terms of screen position, two controls will be rendered on the client side.

It might depend on the Browser, used plugins (which e.g. load additional CSS or Javascript)

You have two options

  • Calculate the geography on the client, then do what you want
  • Try your best and provide a HTML structure that is likely to be rendered the way you want it.

Maybe, in your case, a HTML <table> layout is what you need, given the example.

Solution 2:[2]

This works as expected for me.

The node on which CSS is being applied should be in a scene before the CSS is applied to it.

See the documentation for applyCss():

This method is a no-op if the Node is not in a Scene. The Scene does not have to be in a Stage.

The example application will output to the command line, the following value:

Font[name=Comic Sans MS Bold, family=Comic Sans MS, style=Bold, size=40.0]

Tested on OS X, JavaFX 17.0.1.

Example App

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class FontTemplate extends Application {
    private static final String CUSTOM_FONT_CSS = """
            data:text/css,
            #customFont {
                -fx-font-family: "Comic Sans MS";
                -fx-font-size: 40px;
                -fx-font-weight: bold;
                -fx-text-fill: red;
            }
            """;

    @Override
    public void start(Stage stage) throws Exception {
        Label label = new Label("Label");
        label.getStylesheets().add(CUSTOM_FONT_CSS);
        label.setId("customFont");

        Scene scene = new Scene(label);
        label.applyCss();

        System.out.println(label.getFont());

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

    public static void main(String[] args) {
        launch(args);
    }
}

Solution 3:[3]

Thank you very much for your answer. I completely forgot that a dynamically created label needs to be related in some way to the scene for which the style file is being set up: scene.getStylesheets().add(relative_path_to_css_file)

In your example You assigned a style to Label as CUSTOM_FONT_CSS String, but I need to read the style from the css file that is assigned to the scene, so I used this trick:

  Label label = new Label();
  somePane.getChildren().add(label); //some Pane located on the scene (from .fxml)
  label.setId("myCustomFont");
  label.applyCss();
  Font myFont = label.getFont();
  somePane.getChildren().remove(label); //cleaning

In this way, the Label becomes associated with the scene and the style file, which will make setting some #id contained in the css file work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcel
Solution 2
Solution 3 Marogo