javafx virtual keyboard 자동 바운스 간격 조절 처리.

Posted by sub3
2017.05.12 02:02 개발/JavaFX
하단 텍스트 필드를 선택시 키보드를 밀려오면서 이상하게 배치되는 현상을 고쳐 수정했다.

샘플 코드는 다음과 같다.

package javafx;

import java.util.Iterator;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Bounds;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.PopupWindow;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.Window;

public class MainFXVK extends Application {

private PopupWindow keyboard;

private final Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
private final Rectangle2D bounds = Screen.getPrimary().getBounds();

public static void main(String[] args) {
        Application.launch(args);
    }
@Override
public void start(Stage stage) {
////실행전 설정 - run configurations - arguments - VM arguments  
// -Dcom.sun.javafx.isEmbedded=false
// -Dcom.sun.javafx.touch=true
// -Dcom.sun.javafx.virtualKeyboard=javafx
   TextField tfComment   = new TextField();
   TextField tfComment1  = new TextField();
   TextField tfComment2  = new TextField();
   TextField tfComment3  = new TextField();
   TextField tfComment4  = new TextField();
   TextField tfComment5  = new TextField();
   TextField tfComment6  = new TextField();
   TextField tfComment7  = new TextField();
   TextField tfComment8  = new TextField();
   TextField tfComment9  = new TextField();
   TextField tfComment10 = new TextField();
   TextField tfComment11 = new TextField();
   TextField tfComment12 = new TextField();
   TextField tfComment13 = new TextField();
   TextField tfComment14 = new TextField();
   TextField tfComment15 = new TextField();
   TextField tfComment16 = new TextField();
   TextField tfComment17 = new TextField();
   TextField tfComment18 = new TextField();

   setKeyboardPosition(stage, tfComment);
   setKeyboardPosition(stage, tfComment1);
   setKeyboardPosition(stage, tfComment2);
   setKeyboardPosition(stage, tfComment3);
   setKeyboardPosition(stage, tfComment4);
   setKeyboardPosition(stage, tfComment5);
   setKeyboardPosition(stage, tfComment6);
   setKeyboardPosition(stage, tfComment7);
   setKeyboardPosition(stage, tfComment8);
   setKeyboardPosition(stage, tfComment9);
   setKeyboardPosition(stage, tfComment10);
   setKeyboardPosition(stage, tfComment11);
   setKeyboardPosition(stage, tfComment12);
   setKeyboardPosition(stage, tfComment13);
   setKeyboardPosition(stage, tfComment14);
   setKeyboardPosition(stage, tfComment15);
   setKeyboardPosition(stage, tfComment16);
   setKeyboardPosition(stage, tfComment17);
   setKeyboardPosition(stage, tfComment18);
   
   
   VBox vbox = new VBox();
   vbox.getChildren().addAll(
    tfComment  
    ,tfComment1 
    ,tfComment2 
    ,tfComment3 
    ,tfComment4 
    ,tfComment5 
    ,tfComment6 
    ,tfComment7 
    ,tfComment8 
    ,tfComment9 
    ,tfComment10
    ,tfComment11
    ,tfComment12
    ,tfComment13
    ,tfComment14
    ,tfComment15
    ,tfComment16
    ,tfComment17
    ,tfComment18
    );
   
   BorderPane borderPane = new BorderPane(new Button("virtual keyboard Down"));
   borderPane.setBottom(vbox);

   Scene scene = new Scene(borderPane);

   stage.setScene(scene);
   stage.setX(visualBounds.getMinX());
   stage.setY(visualBounds.getMinY());
   stage.setWidth(visualBounds.getWidth());
   stage.setHeight(visualBounds.getHeight());
   
   stage.show();
    
}
public void setKeyboardPosition(Stage stage, TextField textField) {
textField.focusedProperty().addListener((textFieldObj,oldValue,newValue)->{
Bounds textFieldOffset = textField.localToScene(textField.getBoundsInLocal());
       if( keyboard == null ) {
        keyboard = getPopupWindow();
       
           Platform.runLater(()->{
           
            int keyboardGetY = (int) bounds.getHeight() - (int) keyboard.getHeight();
           
            if ( textFieldOffset.getMaxY() > keyboardGetY) {
            stage.setY( - (int)keyboard.getHeight() );
            } else {
            stage.setY( 0 );
            }
           
           });
   }
       
       if (!newValue ) {
        keyboard = null;
       }
        });
}
private PopupWindow getPopupWindow() {

   @SuppressWarnings("deprecation") 
   final Iterator<Window> windows = Window.impl_getWindows();

   while (windows.hasNext()) {
       final Window window = windows.next();
       if (window instanceof PopupWindow) {
           if(window.getScene()!=null && window.getScene().getRoot()!=null){ 
               Parent root = window.getScene().getRoot();
               if(root.getChildrenUnmodifiable().size()>0){
                   Node popup = root.getChildrenUnmodifiable().get(0);
                   if(popup.lookup(".fxvk")!=null){
                       return (PopupWindow)window;
                   }
               }
           }
           return null;
       }
   }
   return null;
}

     
}



저작자 표시
신고
이 댓글을 비밀 댓글로