JavaFX ColorPicker不会调出CustomColor弹出对话框

时间:2014-11-05 23:13:48

标签: java user-interface javafx

所以我正在为我的一个项目开发GUI,我正在尝试设置一个JavaFX ColorPicker,以允许用户设置矩形的填充颜色。 ColorPicker出现并允许用户选择其中一种默认颜色,但是每当用户单击应打开“自定义颜色”弹出对话框的“自定义颜色”链接时,都会引发NullPointerException。我目前设置了当用户右键单击矩形时,会出现一个ContextMenu,其中一个MenuItem包含ColorPicker。

ContextMenu contextMenu = new ContextMenu();
MenuItem changeColor = new MenuItem();
ColorPicker colorPicker = new ColorPicker();
contextMenu.getItems().add(changeColor);
changeColor.setGraphic(colorPicker);

for(int i = 0; i < customColors.getChildren().size(); i++) {
        customColors.getChildren().get(i).setOnMouseClicked(new EventHandler<MouseEvent>(){
            @Override
            public void handle(MouseEvent e){
                selectedRectangle.setStrokeWidth(1);
                selectedRectangle = (Rectangle)e.getSource();
                ((Rectangle)e.getSource()).setStroke(Color.BLACK);
                ((Rectangle)e.getSource()).setStrokeWidth(4);
                ColorPaletteModel.getInstance().setSelectedIndex(16+customColors.getChildren().indexOf(e.getSource()));
                if(e.getButton() == MouseButton.SECONDARY){
                    contextMenu.show((Rectangle)e.getSource(),Side.TOP,0,0);
                }
            }
        });

这是您尝试单击“自定义颜色”链接时显示的异常线程:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.stage.Window$9.invalidated(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at javafx.stage.Window.setShowing(Unknown Source)
at javafx.stage.Window.show(Unknown Source)
at javafx.stage.Stage.show(Unknown Source)
at com.sun.javafx.scene.control.skin.CustomColorDialog.show(Unknown Source)
at com.sun.javafx.scene.control.skin.ColorPalette$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.ColorPalette$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Hyperlink.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1558600329.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

任何帮助都会很棒。感谢。

编辑:我升级到Java 8.25,但没有帮助。似乎问题可能在于我如何将ColorPicker放在ContextMenu中的MenuItem中,因为我在一个空框架中尝试了ColorPicker并且它工作正常。下面是一个显示他问题的MCVE示例。

package test;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Side;
import javafx.scene.control.ColorPicker;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Test extends Application {

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Test");

    Rectangle rect = new Rectangle(25,25,Color.LIGHTGREY);
    ColorPicker colorPicker = new ColorPicker();
    ContextMenu contextMenu = new ContextMenu();
    MenuItem changeColor = new MenuItem();

    changeColor.setGraphic(colorPicker);
    contextMenu.getItems().add(changeColor);

    rect.setOnMouseClicked(new EventHandler<MouseEvent>(){
        @Override
        public void handle(MouseEvent e){
            ((Rectangle)e.getSource()).setStroke(Color.BLACK);
            ((Rectangle)e.getSource()).setStrokeWidth(4);
            if(e.getButton() == MouseButton.SECONDARY){
                contextMenu.show((Rectangle)e.getSource(),Side.TOP,0,0);
            }
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(rect);
    primaryStage.setScene(new Scene(root, 300, 250));
    primaryStage.show();
}

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

0 个答案:

没有答案