i am facing problem in navigating to the new screen on clicking the image(imageview). i have already written a code for switching between the screens using ActionEvent. now i want to navigate the screens on clicking the image. i tried doing the same but its giving type mismatch error.
my fxml looks like
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="299.0" prefWidth="439.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="multiplescreen.HomeScreenController">
<children>
<Button fx:id="button" layoutX="40.0" layoutY="245.0" onAction="#goToScreen1" text="Logout" />
<Button layoutX="321.0" layoutY="245.0" mnemonicParsing="false" onAction="#Exit" text="Exit" />
<Label layoutX="170.0" layoutY="30.0" text="Home Screen" />
<ImageView id="project" fx:id="project" fitHeight="62.0" fitWidth="85.0" layoutX="31.0" layoutY="105.0" onMouseClicked="#goToProjectCreationScreen" pickOnBounds="true" preserveRatio="true">
<!--<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/Project.png" />
</image>-->
</ImageView>
<ImageView fitHeight="54.0" fitWidth="75.0" layoutX="127.0" layoutY="109.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/Execute.png" />
</image>
</ImageView>
<ImageView fitHeight="62.0" fitWidth="76.0" layoutX="212.0" layoutY="105.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/AccountInfo.jpg" />
</image>
</ImageView>
<ImageView fitHeight="62.0" fitWidth="84.0" layoutX="307.0" layoutY="103.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="file:/C:/Users/user1/Documents/NetBeansProjects/Application1/Images/help.png" />
</image>
</ImageView>
<Label layoutX="49.0" layoutY="173.0" text="Projects" />
<Label layoutX="141.0" layoutY="173.0" text="Execute" />
<Label layoutX="230.0" layoutY="173.0" text="Account Info" />
<Label layoutX="336.0" layoutY="173.0" text="Help" />
</children>
</AnchorPane>
and my controller looks like
public class HomeScreenController implements Initializable,ControlledScreen,EventHandler {
ScreensController myController;
#FXML
private Label label;
#FXML
private ImageView project;
#FXML
private void goToScreen1(ActionEvent event) {
myController.setScreen(MultipleScreen.screen1ID);
}
#FXML
public void goToProjectCreationScreen(ActionEvent event) {
Image image=new Image(getClass().getResourceAsStream("Project.png"));
project.setImage(image);
project.addEventFilter(javafx.scene.input.MouseEvent.MOUSE_CLICKED, new EventHandler<javafx.scene.input.MouseEvent>() {
#Override
public void handle(javafx.scene.input.MouseEvent event) {
//To change body of generated methods, choose Tools | Templates.
System.out.println("clicked");
myController.setScreen(MultipleScreen.screen5ID);
event.consume();
}
});
//myController.setScreen(MultipleScreen.screen5ID);
}
#FXML
private void Exit(ActionEvent event) {
Platform.exit();
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
#Override
public void setScreenParent(ScreensController screenParent) {
myController=screenParent;
}
#Override
public void handle(Event event) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
After excecuting I am getting the exception on clicking the image :
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
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)
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.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$151/20733739.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/17230114.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
The error says it all. The method goToProjectCreationScreen() is called on onMouseClicked event, which expects a MouseEvent.
So the method should accept MouseEvent instead of ActionEvent
Related
I am working on image editor using JavaFX FXML ,scene builder 2.0 .I am getting this error continuously where my scene builder doesn't read FXML file when double clicking on it. I haven't used an other other libraries or anything and have tried opening particular FXML from scene builder directly as well.
I have tried using possible solutions on this question but non seems to work for me.
enter image description here
package imageeditor;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import javax.imageio.ImageIO;
import imageeditor.CommandCenter;
import javafx.scene.control.ColorPicker;
public class FXMLDocumentController implements Initializable {
// #FXML
// private Edit Images = new Edit();
// #FXML
// private ImageView ImageView,i ;
// #FXML private ImageView mImageView;
#FXML
private AnchorPane mAnchorPane;
#FXML
private ImageView mImageView;
#FXML
private Button btnUndo;
#FXML
private Button btnRedo;
#FXML
private Button exit;
#FXML
private Button reset;
#FXML
private Button uploadbtn,savebtn;
#FXML
private Label l1,l2,l3,l4,l5,l6,l7;
#FXML
private Slider bright;
#FXML
private Slider gauss;
#FXML
private Slider hue;
#FXML
private Slider saturation;
#FXML
private Slider contrast;
#FXML
private Button cropbtn ;
#FXML
private Button stickersbtn ;
#FXML
private Button textbtn ;
// #FXML
// private Button ;
#FXML
private ColorPicker framecolour ;
private ColorAdjust AdjustEffect = new ColorAdjust();
// CommandCenter CommandCenter= new CommandCenter();
#FXML
private void LoadImage(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
File file = fileChooser.showOpenDialog(null);
try
{
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
mImageView.setImage(image);
Image currentImage = getSnapshot();
CommandCenter.getInstance().setImageAndView(currentImage);
CommandCenter.getInstance().setOriginalImage(currentImage);
// CommandCenter.getInstance().setImageView(mImageView);
// CommandCenter.getInstance().setOriginalImage(image);
} catch (IOException ex) {
}
}
#FXML
private void SaveImage(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save Image");
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
File file = fileChooser.showSaveDialog(null);
if (file != null) {
try {
BufferedImage bImage = SwingFXUtils.fromFXImage(mImageView.snapshot(null, null), null);
ImageIO.write(bImage,"png", file);
} catch (IOException ex) {
}
}
}
//#FXML void quit(ActionEvent event) {
// System.exit(0);
// }
#FXML void undo(ActionEvent event) {
undo();
}
#FXML void redo(ActionEvent event) {
redo();
}
// save image as snapshot
private Image getSnapshot() {
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setViewport(mImageView.getViewport());
// snapshotParameters.setViewport(new Rectangle2D(mImageView.getX(), mImageView.getY(), mImageView.getImage().getWidth(), mImageView.getImage().getHeight()));
return mAnchorPane.snapshot(snapshotParameters, null);
}
private void undo() {
if (CommandCenter.getInstance().hasUndoImage()) {
Image currentImage = getSnapshot();
CommandCenter.getInstance().addRedoImage(currentImage);
Image undoImage = CommandCenter.getInstance().getUndoImage();
resetEffectsSliders();
CommandCenter.getInstance().setImageAndView(undoImage);
mImageView.setImage(undoImage);
enableRedo();
if (!CommandCenter.getInstance().hasUndoImage()) {
disableUndo();
}
}
}
// redo action
private void redo() {
System.out.println("redo image added");
if (CommandCenter.getInstance().hasRedoImage()) {
Image currentImage = getSnapshot();
CommandCenter.getInstance().addUndoImage(currentImage);
Image redoImage = CommandCenter.getInstance().getRedoImage();
resetEffectsSliders();
CommandCenter.getInstance().setImageAndView(redoImage);
mImageView.setImage(redoImage);
enableUndo();
if (!CommandCenter.getInstance().hasRedoImage()) {
disableRedo();
}
}
}
// update the image and associated properties
private void updateImageAndProperties() {
CommandCenter.getInstance().storeLastImageAsUndo();
CommandCenter.getInstance().clearRedoImages(); // new "path" so clear redo images
disableRedo();
enableUndo();
Image currentImage = getSnapshot();
CommandCenter.getInstance().setImageAndView(currentImage);
resetEffectsSliders();
mImageView.setImage(currentImage);
}
// start over with original image
private void startOver() {
resetEffectsSliders();
Image originalImage = CommandCenter.getInstance().getOriginalImage();
CommandCenter.getInstance().setImageAndView(originalImage);
mImageView.setImage(originalImage);
CommandCenter.getInstance().clearRedoImages();
CommandCenter.getInstance().clearUndoImages();
disableUndo();
disableRedo();
}
private void resetEffectsSliders() {
bright.setValue(0);
contrast.setValue(0.0);
hue.setValue(0.0);
saturation.setValue(0.0);
}
// enable undo buttons
private void enableUndo() {
btnUndo.setDisable(false);
}
// disable undo buttons
private void disableUndo() {
btnUndo.setDisable(true);
}
// enable redo buttons
private void enableRedo() {
btnRedo.setDisable(false);
}
// disable redo buttons
private void disableRedo() {
btnRedo.setDisable(true);
}
// enable start over buttons
private void enableStartOver() {
reset.setDisable(false);
}
#Override
public void initialize(URL url, ResourceBundle rb) {
CommandCenter.getInstance().setImageView(mImageView);
// Images.GaussSliderEvent(gauss);
// Images.BrightSliderEvent(bright);
// Images.SaturationSliderEvent(saturation);
// Images.HueSliderEvent(hue);
// Images.ContrastSliderEvent(contrast);
mImageView.setEffect(AdjustEffect);
// take a snapshot to set as initial image
Image initialImage = getSnapshot();
mImageView.setImage(initialImage);
CommandCenter.getInstance().setImageView(mImageView);
CommandCenter.getInstance().setOriginalImage(initialImage);
CommandCenter.getInstance().setImageAndView(initialImage);
// brightness slider
bright.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setBrightness(newValue.doubleValue());
mImageView.setEffect(AdjustEffect);
updateImageAndProperties();
});
// hue slider
hue.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setHue(newValue.doubleValue());
mImageView.setEffect(AdjustEffect);
updateImageAndProperties();
});
// saturation slider
saturation.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setSaturation(newValue.doubleValue());
//Image.setEffect(AdjustEffect);
updateImageAndProperties();
});
// contrast slider
contrast.valueProperty().addListener((observable, oldValue, newValue) -> {
AdjustEffect.setContrast(newValue.doubleValue());
// Image.setEffect(AdjustEffect);
updateImageAndProperties();
});
// gauss slider
gauss.valueProperty().addListener((observable, oldValue, newValue) -> {
// gauss.setRadius(newValue.doubleValue()*100);
// Image.setEffect(gauss);
updateImageAndProperties();
});
}
}
Java FXML file is
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="650.0" prefWidth="1150.0" style="-fx-background-color: #FFFFF0;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="imageeditor.FXMLDocumentController">
<children>
<Button fx:id="uploadbtn" layoutX="797.0" layoutY="24.0" onAction="#LoadImage" opacity="0.75" style="-fx-background-color: #800000;" text="UPLOAD IMAGE" textFill="WHITE">
<font>
<Font name="Bell MT Bold" size="18.0" />
</font></Button>
<ImageView fx:id="ImageView" fitHeight="639.0" fitWidth="927.0" layoutX="201.0" layoutY="94.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#BG.jpg" />
</image></ImageView>
<Button fx:id="savebtn" layoutX="1007.0" layoutY="23.0" mnemonicParsing="false" onAction="#SaveImage" opacity="0.75" prefHeight="34.0" prefWidth="105.0" style="-fx-background-color: #800000;" text="SAVE" textFill="WHITE">
<font>
<Font name="Bell MT Bold" size="18.0" />
</font></Button>
<Button fx:id="resetbtn" layoutX="54.0" layoutY="576.0" text="RESET" />
<Button fx:id="undobtn" layoutX="25.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" text="UNDO" textFill="WHITE" />
<Button fx:id="redobtn" layoutX="115.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" textFill="WHITE"text="REDO" />
<Button fx:id="cropbtn" layoutX="63.0" layoutY="143.0" mnemonicParsing="false" text="CROP" />
<ColorPicker fx:id="framecolour" layoutX="52.0" layoutY="414.0" />
<Button fx:id="stickersbtn" layoutX="24.0" layoutY="516.0" mnemonicParsing="false" text="ADD STICKERS" />
<Button fx:id="textbtn" layoutX="44.0" layoutY="470.0" mnemonicParsing="false" text="ADD TEXT" />
<Slider fx:id="gauss" blockIncrement="0.1" layoutX="42.0" layoutY="315.0" max="1.0" min="0" />
<Slider fx:id="contrast" blockIncrement="0.01" layoutX="42.0" layoutY="237.0" max="1.0" min="-1.0" />
<Slider fx:id="hue" blockIncrement="0.01" layoutX="42.0" layoutY="199.0" max="1.0" min="-1.0" />
<Slider fx:id="saturation" blockIncrement="0.01" layoutX="42.0" layoutY="277.0" max="1.0" min="-1.0" />
<Slider fx:id="bright" blockIncrement="0.01" layoutX="38.0" layoutY="355.0" max="1.0" min="-1.0" />
<Slider fx:id="frame" blockIncrement="0.01" layoutX="42.0" layoutY="393.0" max="1.0" min="-1.0" />
<ImageView fitHeight="63.0" fitWidth="91.0" layoutX="31.0" layoutY="13.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="#logo.png" />
</image>
</ImageView>
<Label fx:id="l1" layoutX="140.0" layoutY="18.0" prefHeight="51.0" prefWidth="451.0" text="IMAGE EDITOR AND ENHANCER" textFill="#830c0c">
<font>
<Font name="Artifakt Element Black Italic" size="29.0" />
</font>
</Label>
<Label fx:id="l2" layoutX="81.0" layoutY="182.0" text="hue" />
<Label fx:id="l3" layoutX="76.0" layoutY="220.0" text="contrast" />
<Label fx:id="l4" layoutX="68.0" layoutY="258.0" text="saturation" />
<Label fx:id="l5" layoutX="76.0" layoutY="298.0" text="gauss" />
<Label fx:id="l6" layoutX="74.0" layoutY="331.0" text="brightness" />
<Label fx:id="l7" layoutX="74.0" layoutY="376.0" text="frame" />
</children>
</AnchorPane>
SceneBuilder bring a stacktrace if you press show details button
Details :
java.io.IOException: org.xml.sax.SAXParseException; lineNumber: 28; columnNumber: 149; Element type "Button" must be followed by either attribute specifications, ">" or "/>".
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueLoader.load(GlueLoader.java:93)
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueLoader.load(GlueLoader.java:76)
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueDocument.<init>(GlueDocument.java:54)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.<init>(FXOMDocument.java:84)
at com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.<init>(FXOMDocument.java:108)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.updateFxomDocument(EditorController.java:2560)
at com.oracle.javafx.scenebuilder.kit.editor.EditorController.setFxmlTextAndLocation(EditorController.java:763)
at com.oracle.javafx.scenebuilder.app.DocumentWindowController.loadFromFile(DocumentWindowController.java:389)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp.performOpenFiles(SceneBuilderApp.java:668)
at com.oracle.javafx.scenebuilder.app.SceneBuilderApp$1.invalidated(SceneBuilderApp.java:520)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(Unknown Source)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
at javafx.base/javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(Unknown Source)
at javafx.base/javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(Unknown Source)
at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.base/javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at com.oracle.javafx.scenebuilder.kit.library.user.UserLibrary.lambda$updateFirstExplorationCompleted$7(UserLibrary.java:371)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: org.xml.sax.SAXParseException; lineNumber: 28; columnNumber: 149; Element type "Button" must be followed by either attribute specifications, ">" or "/>".
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.seekCloseOfStartTag(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.oracle.javafx.scenebuilder.kit.fxom.glue.GlueLoader.load(GlueLoader.java:91)
... 23 more
java.io.IOException: org.xml.sax.SAXParseException; lineNumber: 28; columnNumber: 149;
Line 28:
<Button fx:id="redobtn" layoutX="115.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" textFill="WHITE"text="REDO" />
Error: there is no space in between textFill and text attributes , so Compiler can't read properly button tag
fix line 28 :
<Button fx:id="redobtn" layoutX="115.0" layoutY="94.0" mnemonicParsing="false" style="-fx-background-color: #800000;" textFill="WHITE" text="REDO" />
Now scenebuilder can open that file
I have been trying to access the imageview in JavaFXML Controller. I have set the imageview fx:id = BWBHQ in the scene builder.
And I'm trying to access it so-
image= new javafx.scene.image.Image(getClass().getResource("dragon.png").toExternalForm());
System.out.println(image.getHeight());//Image is getting loaded
BWBHQ.setImage(image);//null pointer error
I cant figure out why there is a null pointer error.
Please help!!
This is my Application Class:
public class JavaFXApplication2 extends Application {
public static Stage mainStage;
public static Scene scene1,scene2,scene3,scene4;
#Override
public void start(Stage stage) throws Exception {
Parent root2 = FXMLLoader.load(getClass().getResource("InputScreen.fxml"));
scene2 = new Scene(root2);
Parent root4 = FXMLLoader.load(getClass().getResource("GameScreen.fxml"));
scene4 = new Scene(root4);
mainStage=stage;
stage.setScene(scene1);
stage.show();
}
}
This is my FXMLController class
public class FXMLDocumentController implements Initializable {
#FXML
public ImageView BWBHQ ;
#FXML
public void handleInputScreenButtonAction(ActionEvent event) {
initializeWorld();
JavaFXApplication2.mainStage.setScene(JavaFXApplication2.scene4);
}
public void initializeWorld()
{
//create cities
javafx.scene.image.Image image;
image= new javafx.scene.image.Image(getClass().getResource("dragon.png").toExternalForm());
System.out.println(image.getHeight());
BWBHQ.setImage(image);
}
}
This is the GameScreen.fxml that contains the imageview BWBHQ
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="463.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.FXMLDocumentController">
<children>
<ImageView fx:id="BWBHQ" fitHeight="25.0" fitWidth="25.0" layoutX="722.0" layoutY="190.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="RW1BHQ" fitHeight="25.0" fitWidth="25.0" layoutX="674.0" layoutY="190.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
This is the error
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.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$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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:498)
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:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 45 more
Caused by: java.lang.NullPointerException
at javafxapplication2.FXMLDocumentController.initializeWorld(FXMLDocumentController.java:118)
at javafxapplication2.FXMLDocumentController.handleInputScreenButtonAction(FXMLDocumentController.java:99)
... 55 more
I'm new to JavaFX. I've been hunting for answers to these two issues all day. The two fixes I found for the NoSuchMethodException were to make sure the TableView had an fx:id and to make sure it had a constructor without parameters. Made sure I had both of those two things fixed.
The instances cannot be created by FXML Loader shows up when you uncomment the two Inhouse lines in the FXML. I didn't post the full code files because they are huge.
FXML:
<TableView fx:id="partTable" styleClass="dataTable">
<columns>
<TableColumn text="Part ID">
<cellValueFactory>
<PropertyValueFactory property="id" />
</cellValueFactory>
</TableColumn>
<TableColumn fx:id="nameColumn" text="Part Name">
<cellValueFactory>
<PropertyValueFactory property="name" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Inventory Level">
<cellValueFactory>
<PropertyValueFactory property="instock" />
</cellValueFactory>
</TableColumn>
<TableColumn text="Price/Cost per Unit">
<cellValueFactory>
<PropertyValueFactory property="price" />
</cellValueFactory>
</TableColumn>
</columns>
<items>
<FXCollections fx:factory="observableArrayList">
//<Inhouse name="Part 1" price="5.00" instock="5" max="10" min="1" />
//<Inhouse name="Part 2" price="7.00" instock="2" max="11" min="2" />
</FXCollections>
</items>
<sortOrder>
<fx:reference source="nameColumn" />
</sortOrder>
</TableView>
Controller
#FXML
TextField inhouseTextField;
#FXML
TextField outsourcedTextField;
#FXML
private TableView<Inhouse> partTable;
#FXML
private TextField partNameField;
#FXML
private TextField partInstockField;
#FXML
private TextField partPriceField;
#FXML
private TextField partMaxField;
#FXML
private TextField partMinField;
#FXML
protected void addInhouse(ActionEvent event){
ObservableList<Inhouse> data;
data = partTable.getItems();
data.add(new Inhouse(partNameField.getText(),
Integer.parseInt(partInstockField.getText()),
Double.parseDouble(partPriceField.getText()),
Integer.parseInt(partMaxField.getText()),
Integer.parseInt(partMinField.getText()),
Integer.parseInt(inhouseTextField.getText())
));
partNameField.setText("");
partInstockField.setText(String.valueOf(partInstockField));
partPriceField.setText(String.valueOf(partPriceField));
partMaxField.setText(String.valueOf(partMaxField));
partMinField.setText(String.valueOf(partMinField));
inhouseTextField.setText(String.valueOf(inhouseTextField));
}
Class
public class Inhouse extends Part {
Integer machineID;
Inhouse(){
}
Inhouse(String text, int parseInt, double parseDouble, int parseInt0, int parseInt1, int parseInt2) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
Wall of Errors:
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.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:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8390)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.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$MouseHandler.process(Scene.java:3758)
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$280/709942340.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)
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)
... 46 more
Caused by: java.lang.NullPointerException
at ims.AddPartController.addInhouse(AddPartController.java:76)
... 56 more
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I'm trying to click a button in my GUI and switch to a separate scene within the same stage. This seems simple except that I get a NullPointerException in my Controller class when I click the button. The button is written in the FXML with fx:id="settings" and onAction="#handleSettings". The handleSettings method is located in the controller.
I'm trying to import the primaryStage from Main.java into Controller.java by writing public Stage primaryStage; at the top, but I'm clearly missing something. The GUI launches fine but the error occurs when clicking Settings button. The full code is below along with the error. The error is pointing to this line: primaryStage.setScene(sceneSettings);
Do I need to use get/set for primaryStage somehow? Clearly I'm at level 1 in skills. Anything helps, thanks.
Main.java
import ...
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root, 450, 580);
scene.getStylesheets().add("styleMain.css");
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setTitle("Scene 1");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Controller.java
import ...
public class Controller{
Scene sceneSettings;
public Stage primaryStage;
public void handleFieldData(){
System.out.println("field data");
}
public void handleComments(){
System.out.println("comments");
}
#FXML
public void handleSettings() throws IOException {
Button button2 = new Button("settings");
StackPane layout = new StackPane();
layout.getChildren().add(button2);
sceneSettings = new Scene(layout, 450, 580);
primaryStage.setScene(sceneSettings);
primaryStage.setTitle("Settings Menu");
primaryStage.show();
}
}
sample.fxml
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Controller">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="20.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="20.0" vgrow="SOMETIMES" />
</rowConstraints>
<padding>
<Insets bottom="10.0" left="70.0" right="70.0" top="10.0" />
</padding>
<children>
<Button fx:id="comments" mnemonicParsing="false" onAction="#handleComments" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER">
<padding>
<Insets bottom="15.0" left="50.0" right="50.0" top="90.0" />
</padding>
</Button>
<Button fx:id="settings" mnemonicParsing="false" onAction="#handleSettings" text="Settings" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
<padding>
<Insets bottom="15.0" left="30.0" right="30.0" top="15.0" />
</padding>
</Button>
<Label text="Comments" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="TOP" />
</children>
</GridPane>
Error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.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$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
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$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
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:1771)
... 48 more
Caused by: java.lang.NullPointerException
at Controller.handleSettings(Controller.java:38)
... 58 more
When you load your controller you will need to pass it a reference to the primary stage.
public class Controller{
Scene sceneSettings;
public Stage primaryStage;
...
public void setPrimaryStage(Stage stage) {
this.primaryStage = stage;
}
...
Main
#Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("sample.fxml"));
Parent root = loader.load();
loader.getController().setPrimaryStage(primaryStage);
Scene scene = new Scene(root, 450, 580);
scene.getStylesheets().add("styleMain.css");
primaryStage.initStyle(StageStyle.UTILITY);
primaryStage.setTitle("Scene 1");
primaryStage.setScene(scene);
primaryStage.show();
...
since yesterday, i'm trying to change the color of my dialog title (or maybe all title colors of my application) to get a black or dark color, because it's white color and not readable with grey background.
I still think it should work with css but i can't really find the specific entry for the title color.
I tried something like this in dialog.css but did not work, so commented out:
/*
.root {
-fx-text-base-color: blue;
-fx-text-background-color: green;
-fx-text-inner-color: red;
-fx-selection-bar-text: yellow;
}
*/
Here my Dialog class:
package de.test.dialog;
import java.io.IOException;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class Dialog extends Stage {
public static final String OK_DIALOG = "OK";
private final String OK_XML = "/fxml/dialog_ok.fxml";
public enum DIALOG_ACTION {
BUTTON_1, BUTTON_2, BUTTON_3, NOTHING, CLOSE_WINDOW
}
private DialogController controller = null;
private String message = null;
public Dialog(String name, String ... buttonName) {
String resource = getFXMLResource(name);
if (resource != null) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(resource));
Parent root = (Parent) fxmlLoader.load();
controller = fxmlLoader.getController();
controller.setButtons(buttonName);
setScene(new Scene(root));
}
catch (IOException e) {
e.printStackTrace();
}
}
setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent event) {
System.out.println("Closing?");
}
});
}
private String getFXMLResource(String name) {
String fxmlResource = null;
switch(name) {
case OK_DIALOG:
fxmlResource = OK_XML;
break;
default:
break;
}
return fxmlResource;
}
public Dialog.DIALOG_ACTION getAction() {
if (controller != null) {
return controller.getAction();
}
else {
return DIALOG_ACTION.NOTHING;
}
}
public void setMessage(String sMessage) {
this.message = sMessage;
if (controller != null) {
controller.setMessage(message);
}
}
public void setIcon(Image image) {
if (controller != null) {
controller.setIcon(image);
}
}
}
Dialog fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="176.0" prefWidth="400.0" stylesheets="#styles/dialog.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.test.dialog.DialogController">
<children>
<ImageView fx:id="imgIcon" fitHeight="48.0" fitWidth="48.0" layoutX="8.0" layoutY="25.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="15.0" AnchorPane.topAnchor="25.0">
<image>
<Image url="#../icons/dialog/48a.png" />
</image>
</ImageView>
<Text fx:id="txtMessage" layoutX="85.0" layoutY="45.0" strokeType="OUTSIDE" strokeWidth="0.0" text="TEST" wrappingWidth="300.00" AnchorPane.leftAnchor="94.0" AnchorPane.rightAnchor="15.0" AnchorPane.topAnchor="25.0">
<font>
<Font size="14.0" />
</font>
</Text>
<Button fx:id="btn1" defaultButton="true" layoutX="295.0" layoutY="134.0" mnemonicParsing="false" onAction="#doAction" prefHeight="25.0" prefWidth="90.0" text="OK" AnchorPane.rightAnchor="15.0">
<font>
<Font size="14.0" />
</font>
</Button>
<Button fx:id="btn2" cancelButton="true" layoutX="180.0" layoutY="134.0" mnemonicParsing="false" onAction="#doAction" prefHeight="25.0" prefWidth="90.0" text="Abbrechen" visible="false" AnchorPane.rightAnchor="120.0">
<font>
<Font size="14.0" />
</font>
</Button>
<Button fx:id="btn3" layoutX="102.0" layoutY="134.0" mnemonicParsing="false" onAction="#doAction" prefHeight="25.0" prefWidth="90.0" text="Button 3" visible="false" AnchorPane.rightAnchor="225.0">
<font>
<Font size="14.0" />
</font>
</Button>
</children>
</AnchorPane>
Calling my dialog:
Dialog dialog = new Dialog(Dialog.OK_DIALOG, "Löschen", "Abbrechen");
dialog.initModality(Modality.APPLICATION_MODAL);
dialog.initOwner(((Node)e.getSource()).getScene().getWindow());
dialog.setResizable(false);
dialog.setTitle("Dateianhang löschen");
dialog.setMessage("Wollen Sie die ausgewählte(n) Datei(en) wirklich löschen?");
// Get the Stage.
//Stage stage = (Stage) dialog.getScene().getWindow();
// Add a custom icon.
//stage.getIcons().add(new Image("/icons/dialog/48a.png"));
dialog.showAndWait();
As you can see, i tried to change the title icon (commented out), and that worked. But no chance to change the color of the title.
If i try google whith e.g. javafx, i can see a lot of images with black title colors. So it must be possible to change the color, but i don't know how.
Any suggestions?
Greetings,
Tom
I stand to be corrected but I don't think you can set the color of the NATIVE title bar. I think what you are seeing in the google results are custom (user) made title bars.
Maybe this link is useful to you: https://arnaudnouard.wordpress.com/2013/02/02/undecorator-add-a-better-look-to-your-javafx-stages-part-i/
You can also have a look at the FXControls Dialogs source code and see how they have done it: http://fxexperience.com/controlsfx/features/dialogs/