Java Code Examples for javafx.scene.Scene#addEventFilter()

The following examples show how to use javafx.scene.Scene#addEventFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void fixSpaceAfterDeadKey(Scene scene) {
	scene.addEventFilter( KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
		private String lastCharacter;

		@Override
		public void handle(KeyEvent e) {
			String character = e.getCharacter();
			if(" ".equals(character) &&
				("\u00B4".equals(lastCharacter) ||  // Acute accent
				 "`".equals(lastCharacter) ||       // Grave accent
				 "^".equals(lastCharacter)))        // Circumflex accent
			{
				// avoid that the space character is inserted
				e.consume();
			}

			lastCharacter = character;
		}
	});
}
 
Example 2
Source File: AllSwtFxEvents.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Scene createScene() {
	Scene scene = new Scene(new Group(), 400, 400);
	scene.addEventFilter(InputEvent.ANY, new EventHandler<InputEvent>() {
		@Override
		public void handle(InputEvent event) {
			System.out.println(event);
		}
	});
	return scene;
}
 
Example 3
Source File: MouseDragSnippet.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Scene createScene() {
	// layers
	contentLayer = new Pane();
	contentLayer.setPickOnBounds(true);
	handleLayer = new Pane();
	handleLayer.setPickOnBounds(false);
	feedbackLayer = new Pane();
	feedbackLayer.setPickOnBounds(false);
	feedbackLayer.setMouseTransparent(true);

	stackPane = new StackPane();
	stackPane.getChildren().addAll(contentLayer, handleLayer,
			feedbackLayer);

	// scrolling
	ScrollPane scrollPane = new ScrollPane();
	Group scrollPaneContent = new Group(stackPane);
	scrollPaneContent.setAutoSizeChildren(false);
	scrollPane.setContent(scrollPaneContent);

	// scene
	scene = new Scene(scrollPane, 800, 600);
	scene.widthProperty().addListener(sceneSizeChanged);
	scene.heightProperty().addListener(sceneSizeChanged);
	scene.addEventFilter(MouseEvent.ANY, mouseFilter);

	// initially show contents
	onSceneSizeChange(scene.getWidth(), scene.getHeight());

	return scene;
}
 
Example 4
Source File: AllFxEvents.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Scene createScene() {
	Scene scene = new Scene(new Group(new Rectangle(50, 50)), 400, 400);
	scene.addEventFilter(InputEvent.ANY, new EventHandler<InputEvent>() {
		@Override
		public void handle(InputEvent event) {
			System.out.println(event);
		}
	});
	return scene;
}
 
Example 5
Source File: DragDropGesture.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void doHookScene(Scene scene) {
	// register a drag entered filter for forwarding event to drag/drop
	// policies
	scene.addEventFilter(DragEvent.DRAG_ENTERED, dragEnteredFilter);
	// register a drag exited filter for forwarding event to drag/drop
	// policies
	scene.addEventFilter(DragEvent.DRAG_EXITED, dragExitedFilter);
	// register a drag over filter for forwarding event to drag/drop
	// policies
	scene.addEventFilter(DragEvent.DRAG_OVER, dragOverFilter);
	// register a drag dropped filter for forwarding event to drag/drop
	// policies
	scene.addEventFilter(DragEvent.DRAG_DROPPED, dragDroppedFilter);
}
 
Example 6
Source File: ScrollGesture.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
private void hookScene(Scene scene, IViewer viewer) {
	// register scroll filter
	EventHandler<ScrollEvent> scrollFilter = createScrollFilter(viewer);
	scrollFilters.put(viewer, scrollFilter);
	scrollFilterScenes.put(scrollFilter, scene);
	scene.addEventFilter(ScrollEvent.SCROLL, scrollFilter);
}
 
Example 7
Source File: BetonQuestEditor.java    From BetonQuest-Editor with GNU General Public License v3.0 5 votes vote down vote up
private void createPrimaryStage() throws IOException {
	URL location = getClass().getResource("view/Root.fxml");
	Locale locale = Persistence.getSettings().getLanguage();
	language = ResourceBundle.getBundle("pl.betoncraft.betonquest.editor.resource.lang.lang", locale);
	FXMLLoader fxmlLoader = new FXMLLoader(location, language);
	BorderPane root = (BorderPane) fxmlLoader.load();
	TabsController.setDisabled(true);
	Scene scene = new Scene(root, 1280, 720);
	scene.getStylesheets().add(getClass().getResource("resource/style.css").toExternalForm());
	KeyCombination save = new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN);
	KeyCombination export = new KeyCodeCombination(KeyCode.E, KeyCombination.CONTROL_DOWN);
	scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
		if (save.match(event)) {
			event.consume();
			MainMenuController.getInstance().save();
		} else if (export.match(event)) {
			event.consume();
			MainMenuController.getInstance().export();
		}
	});
	stage.setScene(scene);
	stage.setTitle(language.getString("betonquest-editor"));
	stage.getIcons().add(new Image(getClass().getResourceAsStream("resource/icon.png")));
	stage.setMinHeight(600);
	stage.setMinWidth(800);
	stage.setMaximized(true);
	stage.show();
}
 
Example 8
Source File: ClickDragGesture.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void doHookScene(Scene scene) {
	// register mouse move filter for forwarding events to drag policies
	// that can show a mouse cursor to indicate their action
	scene.addEventFilter(MouseEvent.MOUSE_MOVED,
			indicationCursorMouseMoveFilter);
	// register key event filter for forwarding events to drag policies
	// that can show a mouse cursor to indicate their action
	scene.addEventFilter(KeyEvent.ANY, indicationCursorKeyFilter);
	// register mouse filter for forwarding press, drag, and release
	// events
	scene.addEventFilter(MouseEvent.ANY, mouseFilter);
}
 
Example 9
Source File: DesktopController.java    From CPUSim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Private generic method to open a new window.
 *
 * @param title      - desired title for the window to be created.
 * @param fxmlPath   - path to the fxml file that contains the
 *                   formatting for the window to be opened.
 * @param x          - the horizonal distance from the left edge of the
 *                   desktop window to the left edge of the new window.
 * @param y          - the vertical distance from the top edge of the
 *                   desktop window to the top edge of the new window.
 * @param modality   - The modality of the new window.
 */
private void openDialog(String title, Object controller, String fxmlPath, int x, int y, Modality modality) {
	checkNotNull(controller);
	checkArgument(!Strings.isNullOrEmpty(fxmlPath));
	
    final FXMLLoader fxmlLoader = FXMLLoaderFactory.fromController(controller, fxmlPath);

    final Stage dialogStage = new Stage();
    // Load in icon for the new dialog
    URL url = getClass().getResource("/images/icons/cpusim_icon.jpg");
    Image icon = new Image(url.toExternalForm());
    dialogStage.getIcons().add(icon);
    Pane dialogRoot = null;

    try {
        dialogRoot = fxmlLoader.load();
    } catch (IOException e) {
        // should never happen
        throw new IllegalArgumentException("Unable to load file: " + fxmlPath, e);
    }
    Scene dialogScene = new Scene(dialogRoot);
    dialogStage.setScene(dialogScene);
    //dialogStage.initOwner(stage);
    dialogStage.initModality(modality);
    dialogStage.setTitle(title);
    if (x >= 0 && y >= 0) {
        dialogStage.setX(stage.getX() + x);
        dialogStage.setY(stage.getY() + y);
    }
    // pressing escape key causes the dialog to close without saving changes
    dialogScene.addEventFilter(KeyEvent.KEY_RELEASED, event -> {
        if (event.getCode().equals(KeyCode.ESCAPE)) {
            if (dialogStage.isFocused()) {
                dialogStage.close();
            }
        }
    });
    dialogStage.show();
}
 
Example 10
Source File: TableApp.java    From examples-javafx-repos1 with Apache License 2.0 5 votes vote down vote up
@Override
public void start(Stage primaryStage) throws Exception {

	FXMLLoader fxmlLoader = new FXMLLoader(
			TableApp.class.getResource("/Persons.fxml"),
			null,
			new JavaFXBuilderFactory(),
			new Callback<Class<?>, Object>() {
				@Override
	    		public Object call(Class<?> clazz) {
					return injector.getInstance(clazz);
				}
			});
	
    Parent p = fxmlLoader.load();
    
    PersonsController pc = fxmlLoader.getController();
    
    Scene scene = new Scene(p);
    
    scene.addEventFilter(KeyEvent.KEY_PRESSED, (evt) -> {
    	if( evt.getCode() == KeyCode.DELETE ) {
    		pc.deletePersons();
    	} else if( evt.getCode() == KeyCode.INSERT ) {
    		pc.addPerson();
    	}        	
    });
    
    primaryStage.setTitle( "Person Table App");
    primaryStage.setScene( scene );
    primaryStage.show();
}
 
Example 11
Source File: CloseWindowOnEscape.java    From erlyberly with GNU General Public License v3.0 4 votes vote down vote up
public static void apply(Scene scene, Stage stage) {
    scene.addEventFilter(KeyEvent.KEY_PRESSED, new CloseWindowOnEscape(stage));
}
 
Example 12
Source File: TabDemo.java    From phoebus with Eclipse Public License 1.0 4 votes vote down vote up
@Override
  public void start(final Stage stage)
  {
      // TabPane with some tabs
      final TabPane tabs = new TabPane();
      tabs.setStyle("-fx-background-color: red;");
      for (int i=0; i<3; ++i)
      {
          final Rectangle rect = new Rectangle(i*100, 100, 10+i*100, 20+i*80);
          rect.setFill(Color.BLUE);
          final Pane content = new Pane(rect);
          final Tab tab = new Tab("Tab " + (i+1), content);
          tab.setClosable(false);
          tabs.getTabs().add(tab);
      }
      tabs.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
      tabs.setPrefSize(400, 300);

      final Group widgets = new Group(tabs);
      widgets.setScaleX(0.5);
      widgets.setScaleY(0.5);
      final Group scroll_content = new Group(widgets);
      final ScrollPane scroll = new ScrollPane(scroll_content);
      final Scene scene = new Scene(scroll);
      stage.setTitle("Tab Demo");
      stage.setScene(scene);
      stage.show();

      // Unfortunately, the setup of ScrollPane -> Group -> Group -> TabPane
      // breaks the rendering of the TabPane.
      // While the red background shows the area occupied by TabPane,
      // the actual Tabs are missing..
      System.out.println("See anything?");
      scene.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent event) ->
{
	if (event.getCode() == KeyCode.SPACE)
       {   // .. until 'side' or 'tabMinWidth' or .. are twiddled to force a refresh
           tabs.setSide(Side.BOTTOM);
           tabs.setSide(Side.TOP);
           System.out.println("See it now?");
       }
});
  }
 
Example 13
Source File: KeyButtonEventHandler.java    From jfxvnc with Apache License 2.0 4 votes vote down vote up
public void register(Scene scene) {
  scene.addEventFilter(KeyEvent.KEY_PRESSED, keyEventHandler);
  scene.addEventFilter(KeyEvent.KEY_TYPED, keyEventHandler);
  scene.addEventFilter(KeyEvent.KEY_RELEASED, keyEventHandler);
}
 
Example 14
Source File: ClickDragGesture.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * This method is called upon {@link MouseEvent#MOUSE_RELEASED} events. This
 * method is also called for other mouse events, when a mouse release event
 * was not fired, but was detected otherwise (probably only possible when
 * using the JavaFX/SWT integration).
 *
 * @param target
 *            The event target.
 * @param event
 *            The corresponding {@link MouseEvent}.
 * @param dx
 *            The horizontal displacement from the mouse press location.
 * @param dy
 *            The vertical displacement from the mouse press location.
 */
protected void release(Node target, MouseEvent event, double dx,
		double dy) {
	if (activeViewer == null) {
		return;
	}

	// enable indication cursor event filters outside of
	// press-drag-release gesture
	Scene scene = activeViewer.getRootPart().getVisual().getScene();
	if (scene == null) {
		throw new IllegalStateException(
				"Active viewer's root part visual is not in Scene.");
	}
	scene.addEventFilter(MouseEvent.MOUSE_MOVED,
			indicationCursorMouseMoveFilter);
	scene.addEventFilter(KeyEvent.ANY, indicationCursorKeyFilter);

	// abort processing of this gesture if no policies could be
	// found that can process it
	if (getActiveHandlers(activeViewer).isEmpty()) {
		activeViewer = null;
		return;
	}

	// send release() to all drag policies
	for (IOnDragHandler policy : getActiveHandlers(activeViewer)) {
		policy.endDrag(event, new Dimension(dx, dy));
	}

	// clear active policies before processing release
	clearActiveHandlers(activeViewer);
	activeViewer = null;

	// remove this tool from the domain's execution transaction
	getDomain().closeExecutionTransaction(ClickDragGesture.this);

	// hide indication cursor
	if (indicationCursorPolicy[0] != null) {
		indicationCursorPolicy[0].hideIndicationCursor();
		indicationCursorPolicy[0] = null;
	}
}
 
Example 15
Source File: PinchSpreadGesture.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void doHookScene(Scene scene) {
	EventHandler<ZoomEvent> zoomFilter = createZoomFilter(scene);
	scene.addEventFilter(ZoomEvent.ANY, zoomFilter);
	zoomFilters.put(scene, zoomFilter);
}
 
Example 16
Source File: RotateGesture.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void doHookScene(Scene scene) {
	EventHandler<RotateEvent> rotateFilter = createRotateFilter(scene);
	scene.addEventFilter(RotateEvent.ANY, rotateFilter);
	rotateFilters.put(scene, rotateFilter);
}
 
Example 17
Source File: HoverGesture.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void doHookScene(Scene scene) {
	EventHandler<MouseEvent> hoverFilter = createHoverFilter();
	scene.addEventFilter(MouseEvent.ANY, hoverFilter);
	hoverFilters.put(scene, hoverFilter);
}