Advertisement
Guest User

TextField caret error

a guest
Nov 13th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.event.EventHandler;
  3. import javafx.geometry.Insets;
  4. import javafx.scene.Group;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Label;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.input.MouseEvent;
  9. import javafx.scene.layout.VBox;
  10. import javafx.scene.text.Font;
  11. import javafx.stage.Stage;
  12.  
  13. public class test extends Application {
  14.  
  15.     public static void main(String[] args) {
  16.         launch(args);
  17.     }
  18.  
  19.     @Override
  20.     public void start(Stage stage) {
  21.         Scene scene = new Scene(new Group());
  22.         stage.setTitle("TextField Sample");
  23.         stage.setWidth(450);
  24.         stage.setHeight(550);
  25.  
  26.         final Label label = new Label("click here");
  27.         final TextField temp = new TextField();
  28.         label.setOnMouseClicked(new EventHandler<MouseEvent>() {
  29.  
  30.             @Override
  31.             public void handle(MouseEvent arg0) {
  32.                 //I put the text
  33.                 temp.setText("this is a test");
  34.                 //Change the graphic
  35.                 label.setGraphic(temp);
  36.                
  37.                 temp.requestFocus();
  38.                 // I want the caret to be at the end. In reality it will
  39.                 // be but at screen it will be at the beginning the first time.
  40.                 temp.end();
  41.             }
  42.         });
  43.         label.setFont(new Font("Arial", 20));
  44.  
  45.         final VBox vbox = new VBox();
  46.         vbox.setSpacing(5);
  47.         vbox.setPadding(new Insets(10, 0, 0, 10));
  48.         vbox.getChildren().addAll(label);
  49.  
  50.         ((Group) scene.getRoot()).getChildren().addAll(vbox);
  51.  
  52.         stage.setScene(scene);
  53.         stage.show();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement