首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX Toggle MenuButton

JavaFX Toggle MenuButton
EN

Stack Overflow用户
提问于 2018-04-13 08:10:27
回答 1查看 625关注 0票数 0

是否有允许切换所选状态的JavaFX MenuButton (特别是SplitMenuButton)?Swing等价于您可以创建的OpenIDE JToggleButton:

代码语言:javascript
复制
JToggleButton button = DropDownButtonFactory.createDropDownToggleButton(icon, menu)

因此,当用户单击操作区域时,按钮的选定状态除了触发与ButtonBase.onAction属性相关的内容外,还应该切换。单击箭头应显示下拉菜单,如预期。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-13 10:59:15

好吧,我想我已经想出办法了。您需要创建一个名为ToggleSplitMenuButton的新类,它从SplitMenuButton扩展而来:

代码语言:javascript
复制
import java.net.URL;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.BooleanPropertyBase;
import javafx.collections.ObservableList;
import javafx.css.PseudoClass;
import javafx.event.ActionEvent;
import javafx.scene.AccessibleAttribute;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SplitMenuButton;

public class ToggleSplitMenuButton extends SplitMenuButton {

    private static final String STYLESHEET = "toggle-split-menu-button.css";

    /***************************************************************************
     *                                                                         *
     * Constructors                                                            *
     *                                                                         *
     **************************************************************************/

    public ToggleSplitMenuButton() {
        super();
        initialize();
    }

    public ToggleSplitMenuButton(MenuItem ... items) {
        super(items);
        initialize();
    }

    private void initialize() {
        Class<?> clazz = getClass();
        URL resource = clazz.getResource(STYLESHEET);
        String stylesheet = resource.toExternalForm();
        ObservableList<String> stylesheets = getStylesheets();
        stylesheets.add(stylesheet);
    }

    /***************************************************************************
     *                                                                         *
     * Properties                                                              *
     *                                                                         *
     **************************************************************************/
    /**
     * Indicates whether this toggle split menu button is selected. This can be
     * manipulated programmatically.
     */
    private BooleanProperty selected;

    public final void setSelected(boolean value) {
        selectedProperty().set(value);
    }

    public final boolean isSelected() {
        return selected == null ? false : selected.get();
    }

    public final BooleanProperty selectedProperty() {
        if (selected == null) {
            selected = new BooleanPropertyBase() {

                @Override
                protected void invalidated() {
                    final boolean selected = get();
                    pseudoClassStateChanged(PSEUDO_CLASS_SELECTED, selected);
                    notifyAccessibleAttributeChanged(AccessibleAttribute.SELECTED);
                }

                @Override
                public Object getBean() {
                    return ToggleSplitMenuButton.this;
                }

                @Override
                public String getName() {
                    return "selected";
                }
            };
        }
        return selected;
    }

    /***************************************************************************
     *                                                                         *
     * Methods                                                                 *
     *                                                                         *
     **************************************************************************/

    @Override
    public void fire() {
        if (!isDisabled()) {
            setSelected(!isSelected());
            fireEvent(new ActionEvent());
        }
    }

    /***************************************************************************
     *                                                                         *
     * Stylesheet Handling                                                     *
     *                                                                         *
     **************************************************************************/

    private static final PseudoClass PSEUDO_CLASS_SELECTED = PseudoClass.getPseudoClass("selected");
}

您还需要创建相应的样式表(称为“切换-拆分-菜单-按钮”),以正确地对组件进行样式设置:

代码语言:javascript
复制
.split-menu-button:selected > .label {
    -fx-background-color:
        -fx-shadow-highlight-color,
        linear-gradient(to bottom, derive(-fx-outer-border, -20%), -fx-outer-border),
        linear-gradient(to bottom,
                derive(-fx-color, -22%) 0%,
                derive(-fx-color, -13%) 20%,
                derive(-fx-color, -11%) 50%);
}
.split-menu-button:selected > .arrow-button {
    -fx-background-color:
        -fx-shadow-highlight-color,
        linear-gradient(to bottom, derive(-fx-outer-border, -20%), -fx-outer-border),
        linear-gradient(to bottom,
                derive(-fx-color, -22%) 0%,
                derive(-fx-color, -13%) 20%,
                derive(-fx-color, -11%) 50%);
}
.split-menu-button:selected:focused > .label {
    -fx-background-color:
        -fx-shadow-highlight-color,
        linear-gradient(to bottom,
            derive(-fx-color, -22%) 0%,
            derive(-fx-color, -13%) 20%,
            derive(-fx-color, -11%) 50%),
        -fx-faint-focus-color,
        linear-gradient(to bottom,
            derive(-fx-color, -22%) 0%,
            derive(-fx-color, -13%) 20%,
            derive(-fx-color, -11%) 50%);
}
.split-menu-button:selected:focused > .arrow-button {
    -fx-background-color:
        -fx-shadow-highlight-color,
        linear-gradient(to bottom,
            derive(-fx-color, -22%) 0%,
            derive(-fx-color, -13%) 20%,
            derive(-fx-color, -11%) 50%),
        -fx-faint-focus-color,
        linear-gradient(to bottom,
            derive(-fx-color, -22%) 0%,
            derive(-fx-color, -13%) 20%,
            derive(-fx-color, -11%) 50%);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49812227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档