【问题标题】:JavaFX : Highlight VBox on mouse click and change color when unfocusedJavaFX:在鼠标单击时突出显示 VBox 并在未聚焦时更改颜色
【发布时间】:2015-11-21 06:27:13
【问题描述】:

我想在进行鼠标单击事件时更改 VBox 的背景(聚焦)颜色,而其他(兄弟)应该更改为其他(未聚焦)颜色。

所以,我在下面这样编码..

首先,我用一些 VBox 制作了一个 ObservableList。

public class ManagerWorldController implements Initializable {

        @FXML
        private BorderPane borderPane;
        @FXML
        private AnchorPane anchorPane;
        @FXML
        private VBox menuVBox1;
        @FXML
        private VBox menuVBox2;
        @FXML
        private VBox menuVBox3;
        @FXML
        private VBox menuVBox4;
        @FXML
        private VBox menuVBox5;
        @FXML
        private VBox menuVBox6;
        @FXML
        private VBox menuVBox7;
        @FXML
        private VBox menuVBox8;
        @FXML
        private VBox menuVBox9;
        @FXML
        private VBox menuVBox10;
        @FXML
        private VBox menuVBox11;

    //    ObservableList<VBox> menuVBoxList = FXCollections
    //            .observableArrayList(menuVBox1, menuVBox2, menuVBox3, menuVBox4, menuVBox5, menuVBox6, menuVBox7, menuVBox8, menuVBox9, menuVBox10, menuVBox11);

        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO

        }

        @FXML
        private void handleHyperLinkOnAction(MouseEvent event) {
            if (event.getSource().equals(menuVBox1)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox1);
            } else if (event.getSource().equals(menuVBox2)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox2);
            } else if (event.getSource().equals(menuVBox3)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox3);
            } else if (event.getSource().equals(menuVBox4)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox4);
            } else if (event.getSource().equals(menuVBox5)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox5);
            } else if (event.getSource().equals(menuVBox6)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox6);
            } else if (event.getSource().equals(menuVBox7)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox7);
            } else if (event.getSource().equals(menuVBox8)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox8);
            } else if (event.getSource().equals(menuVBox9)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox9);
            } else if (event.getSource().equals(menuVBox10)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox10);
            } else if (event.getSource().equals(menuVBox11)) {
                String viewerPath = "/managerworld/viewer/ConnectionViewer.fxml";
                changeScreenOfCenter(viewerPath, menuVBox11);
            } else {
            }

        }

        private void changeScreenOfCenter(String path, VBox menuVBox) {
            VBox getVbox = menuVBox;
            try {
                anchorPane = FXMLLoader.load(getClass().getResource(path));
            } catch (Exception e) {
                System.out.println(e.getStackTrace().toString());
                System.out.println(e.getMessage());
            }
    //        for (VBox vbox : this.menuVBoxList) {
    //            System.out.println(vbox.getId());
    //            if (vbox.getId().equals(getVbox.getId())) {
    //                menuVBox.setStyle("-fx-background-color: #D3D3D3");
    //            } else {
    //                //Change the background color to white on other VBoxes..
    //            }
    //        }
            menuVBox.setStyle("-fx-background-color: #D3D3D3");
            borderPane.setCenter(anchorPane);
        }

    }

然而,一旦我点击menuVBox1,就会出现空指针异常。

Executing G:\JamesProjects\ManagerWorld\dist\run1616043779\ManagerWorld.jar using platform C:\Program Files\Java\jdk1.8.0_40\jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3471)
    at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3399)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3767)
    at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$148/1745970415.get(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:927)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
    ... 31 more
Caused by: java.lang.NullPointerException
    at managerworld.controller.ManagerWorldController.changeScreenOfCenter(ManagerWorldController.java:111)
    at managerworld.controller.ManagerWorldController.handleHyperLinkOnAction(ManagerWorldController.java:66)
    ... 41 more

请帮帮我..

【问题讨论】:

  • 你为什么将这些 VBox 存储在 ObservableList 中?您的应用中如何以及在何处使用 menuVBoxList?
  • 这不是您问题的答案,因此请通过编辑更新上述问题中的代码。
  • 并删除下面的答案
  • 好的,我只想将 vbox 的颜色更改为白色,除非我点击了。比如我点击第一个VBox,它变成灰色,而第二个基本上是白色的,接下来,如果我点击第二个,它变成灰色,第一个变成灰色。
  • AFAIK 只有在没有注入 borderPane 的情况下才会发生该源代码的异常。

标签: javafx javafx-8 observablelist


【解决方案1】:

由于您发布的代码尚未完成运行测试,因此我编写了一个简单的演示应用程序,该应用程序显示如何根据您的问题要求在鼠标单击时设置 vbox 的样式。请花一些时间了解这些内容并将其移植到您自己的应用中。

public class VBoxHighlightOnClickDemo extends Application
{
    // vboxes
    private VBox menuVBox1 = new VBox( new Label( "menuVBox1" ) );
    private VBox menuVBox2 = new VBox( new Label( "menuVBox2" ) );
    private VBox menuVBox3 = new VBox( new Label( "menuVBox3" ) );
    private VBox menuVBox4 = new VBox( new Label( "menuVBox4" ) );
    private VBox menuVBox5 = new VBox( new Label( "menuVBox5" ) );

    // parent vbox that includes all vboxes
    private VBox parentVBox = new VBox( 10, menuVBox1, menuVBox2, menuVBox3, menuVBox4, menuVBox5 );

    // styles used for vboxes
    private final Background focusBackground = new Background( new BackgroundFill( Color.BLUEVIOLET, CornerRadii.EMPTY, Insets.EMPTY ) );
    private final Background unfocusBackground = new Background( new BackgroundFill( Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY ) );
    private final Border border = new Border( new BorderStroke( Color.BLUE, BorderStrokeStyle.SOLID, null, null ) );


    @Override
    public void start( Stage primaryStage )
    {
        boxifyVBoxes();
        Scene scene = new Scene( new HBox( parentVBox ), 300, 300 );
        primaryStage.setScene( scene );
        primaryStage.show();
    }


    private void boxifyVBoxes()
    {
        for ( Node child : parentVBox.getChildren() )
        {
            VBox vb = ( VBox ) child;
            vb.setPadding( new Insets( 10 ) );
            vb.setBorder( border );

            // when vbox is clicked focus on it
            vb.setOnMouseClicked( ( e ) ->
            {
                vb.requestFocus();
            } );

            // use different backgrounds for focused and unfocused states
            vb.backgroundProperty().bind( Bindings
                    .when( vb.focusedProperty() )
                    .then( focusBackground )
                    .otherwise( unfocusBackground )
            );

        }
    }

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

}

输出:

【讨论】:

  • 这是一个非常聪明的例子。
猜你喜欢
  • 1970-01-01
  • 2015-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多