简体   繁体   English

ButtonBar 中的第一个按钮始终保持焦点?

[英]First Button in a ButtonBar is always focused?

I am currently developing a JavaFX application and there I used a ButtonBar with some Buttons.我目前正在开发一个JavaFX应用程序,在那里我使用了一个带有一些按钮的ButtonBar When I run the program, the first Button is always focused.当我运行程序时,第一个Button始终是焦点。 How can I disable that setting?如何禁用该设置?

在此处输入图像描述

PS: I used some CSS code to change the default appearance and that brown box conveys that the button is focused. PS:我使用了一些CSS代码来更改默认外观,并且棕色框表示按钮已聚焦。

You can take away the focus from the button with otherNode.requestFocus() :您可以使用otherNode.requestFocus()将焦点从按钮上移开:

Requests that this Node get the input focus, and that this Node's top-level ancestor become the focused window.请求该节点获得输入焦点,并且该节点的顶级祖先成为焦点 window。 To be eligible to receive the focus, the node must be part of a scene, it and all of its ancestors must be visible, and it must not be disabled.为了有资格获得焦点,节点必须是场景的一部分,它和它的所有祖先都必须是可见的,并且不能被禁用。 If this node is eligible, this function will cause it to become this Scene's "focus owner".如果此节点符合条件,则此 function 将使其成为此场景的“焦点所有者”。 Each scene has at most one focus owner node.每个场景最多有一个焦点所有者节点。 The focus owner will not actually have the input focus, however, unless the scene belongs to a Stage that is both visible and active ( docs ).但是,焦点所有者实际上不会拥有输入焦点,除非场景属于既可见又活跃的 Stage ( docs )。

Like in this example:就像在这个例子中一样:


import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.stage.Stage;

import java.util.stream.IntStream;

public class App extends Application {

    @Override
    public void start(Stage stage) {
        // Create controls:
        ButtonBar buttonBar = new ButtonBar();
        IntStream.range(0, 3).forEach(i -> buttonBar.getButtons().add(new Button("Button " + i)));
        
        // This takes away the focus from the button:
        Platform.runLater(buttonBar::requestFocus); // (Does not have to be the buttonBar itself)
        
        // Prepare and show stage:
        stage.setScene(new Scene(buttonBar));
        stage.show();
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM