Search code examples
javalayoutjavafxjavafx-2pane

When adding a second item to my stackpane, the first item loses its Event/MouseOn. Why? How can I fix? JavaFX


I have a stackpane. When I add a second item to my stack pane, both show up, but I can't click on my first item anymore. It becomes 'unclickable'.

what ever I defined in my .setonmouse does not work. It works for my second item. If I switch the order they are in the stack pane, the other one works, but not both.

is there a fix for this? This is what my program looks like:

I want my 'grid' centered ALWAYS. There are buttons to the left centered in a column, there will be buttons on the right later on, and there will be buttons/Text on top of the grid and buttom in the margins later on too.

I want everything to be clickable.

http://img688.imageshack.us/img688/6025/examplerg.png


Solution

  • StackPane orders items in Z-order: latter above the former. So, your second item gots all mouse clicks and first one (being covered by second) doesn't get anything.

    For layout you've described you can use BorderPane:

    public void start(Stage stage) throws Exception {
        BorderPane root = new BorderPane();
        root.setCenter(new Rectangle(100,100, Color.RED));
        root.setLeft(new Rectangle(10,10, Color.BLUE));
        root.setRight(new Rectangle(10,10, Color.CYAN));
    
        stage.setScene(new Scene(root,300,300));
    
        stage.show();
    }