JavaFX ColorPicker will not display CustomColor popup dialog

So I am currently working on a GUI for one of my projects and I am trying to customize the JavaFX ColorPicker so that the user can set the fill color of the rectangle. The ColorPicker appears and allows the user to choose one of the default colors, however, whenever the user clicks on the Custom Color link that should open the Custom Color dialog box, a NullPointerException is thrown. Currently I have set that when the user right-clicks on the rectangle, a ContextMenu appears with one MenuItem that contains a 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);
                }
            }
        });

      

This is the exception stream that appears when you try to click the Custom Colors link:

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)

      

Any help whatsoever would be great. Thank.

EDIT: I updated to Java 8.25, but it didn't help. It looks like the problem might be the way I am putting the ColorPicker inside the MenuItem inside the ContextMenu because I tried the ColorPicker on an empty frame and it worked fine. Below is an example MCVE that shows what it outputs.

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);
}
}

      

+3


source to share





All Articles