Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with focus lost, hiding panes and button clicks

zonskiMar 28 2012 — edited Mar 29 2012
My application runs on a windows Tablet, so I've built my own ipad-like soft keyboard. When a text field has focus the keyboard pops up, when it loses focus the keyboard automatically hides. This was all pretty easy to implement, however I have a problem that when a field has focus (so the keyboard is showing) and then a button is clicked. The click of the button causes the field to lose focus and so the keyboard gets hidden (which is what I want) - the problem is the loss of focus happens on the 'pressing in' of the button, which means the button moves between the 'pressing in' and the 'releasing' actions, which in turn means the 'release' is not over the button, which means the 'click' never happens!

I can actually trick the system. If I press the button in and hold it, wait for the keyboard to go, then move my mouse (with the button still held in) over the button in its new location, and then finally release the mouse, the click happens. Obviously not something I'm going to be able to get my users to do however.

Can anyone think of a work around for this - is there another way I could link my keyboard's visibility to the focus of text fields without affecting my button clicks?

Here's some really simple code that shows the problem, if you click in the TextField the 'footer' shows. If you click the button the 'footer' hides, but the click handler is not triggered (i.e. there is no log printed to the screen). If you then click the button again with the footer already hidden, you do see log.
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class TestApp extends Application
{
    private BorderPane mainPane;
    private Pane footer;

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

    public void start(Stage stage) throws Exception
    {
        mainPane = new BorderPane();
        mainPane.setPadding(new Insets(10));

        VBox contentArea = new VBox();
        TextField field1 = new TextField("Footer appears when this field has focus");
        field1.focusedProperty().addListener(new ChangeListener<Boolean>() 
        {
            public void changed(ObservableValue<? extends Boolean> source, Boolean wasFocused, Boolean isFocused)
            {
                if (isFocused) 
                {
                    mainPane.setBottom(footer);
                }
                else
                {
                    mainPane.setBottom(null);
                }
            }
        });
        contentArea.getChildren().add(field1);

        Button button = new Button("Test");
        button.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent event)
            {
                System.out.println("Button was pressed");
            }
        });
        contentArea.getChildren().add(button);

        VBox centerArea = new VBox();
        centerArea.setAlignment(Pos.CENTER);
        centerArea.getChildren().add(contentArea);
        mainPane.setCenter(centerArea);

        footer = new HBox();
        footer.setStyle("-fx-padding: 200px; -fx-background-color: #ccffcc;");
        footer.getChildren().add(new Label("This is the footer"));

        Scene scene = new Scene(mainPane, 1024, 768);
        stage.setScene(scene);
        stage.show();
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 26 2012
Added on Mar 28 2012
3 comments
2,518 views