2

Ok, this is my first time using Javafx and Scene Builder but I've run into a problem I can't find a solution for.

Basically, I'm making an app which includes a settings tab in which the user can change the colour theme of the app (i.e a light theme with light coloured background, buttons, tabs, textfields, etc) using radioButtons. So if the user clicks on the "light" radioButton, it should load the "light" css file, if "dark" radioButton is selected, it should change to use the "dark" css file, etc.

In the controller class I have the following:

@FXML
RadioButton rbtnLight;
@FXML
public void colourThemeLight(javafx.event.ActionEvent event)
{ 
 if (rbtnLight.isSelected() == true)
    {
        System.out.println("I'm the chosen button");
        //I'm using println at the moment to test that it's actually recognizing the action performed
    }
}

 @FXML
RadioButton rbtnDark;
 @FXML
public void colourThemeDark(javafx.event.ActionEvent event)
{ 
 if (rbtnDark.isSelected() == true)
    {
        System.out.println("I'm the other chosen button");
        //Again, I'm using println at the moment to test that it's actually recognizing the action performed
    }
}

But it gives a 'RuntimeException: java.lang.reflect.InvocationTargetException' caused by a NullPointerException at the if (rbtnLight.isSelected() = true) and the if (rbtnLight.isSelected() = true statements.

I've also tried using the rbtnLight.isDisabled() == false and .isPressed() == true however both give the same error. Alternatively, I've tried if (event.getSource() == rbtnTLightS) but that does nothing, literally. No errors, but no message either.

In Scene Builder, I have loaded the style sheets to the main windows, not the individual radioButtons in the settings tab.

(Almost done...)

If and when that does get working, how would I get the right css file loaded? I have one method I've found which is the following:

File file = new File("path");
String css1 = file.toString();
mainS.getStyleClass().add(css1); //mainS being the main screen in sceneBuilder which everything is placed on

But I'm skeptical whether or not that would work.

Any and all help would be much appreciated. Sorry for the essay :)

EDIT: complete stack trace -

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.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.ToggleButton.fire(ToggleButton.java:256)
at javafx.scene.control.RadioButton.fire(RadioButton.java:113)
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.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$257/436071628.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)
... 59 more
Caused by: java.lang.NullPointerException
at settings.FXMLDocumentController.colourThemeLight(FXMLDocumentController.java:75)
... 69 more
Grey
  • 45
  • 7
  • Can you add the complete stacktrace? From what you have posted, I am of the opinion that the `rbtnLight` is still **null** and you are getting a `NullPointerException`. – ItachiUchiha Jul 09 '15 at 12:05
  • @ItachiUchiha Added :) – Grey Jul 09 '15 at 13:04
  • Be sure that those radioButtons in FXML file has correct fx:ids. – Uluk Biy Jul 09 '15 at 13:07
  • @UlukBiy Ahh crap... The fx:id gets me again... Keep forgetting about it. Now that it's recognizing an action, would the method I posted about loading a different css file work? Or am I adding it to the wrong item? – Grey Jul 09 '15 at 15:25

1 Answers1

1

Would the method I posted about loading a different css file work?

No.

mainS.getStyleClass().add(css1);

will not work, since getStyleClass() accepts the CSS selectors defined in CSS style sheet file, and not its path or name.

It should be

mainS.getStyleSheets().add(css1);

See this example of changing css at runtime.

Community
  • 1
  • 1
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • Wow, thanks! I'm getting an error by the root.getChildren() though, `error: getChildren() has protected access in Parent` my parent is a Split Pane. I've read that this could be a reason, but I don't have a Border Pane as an option to wrap it in and putting it into a Anchor, Grid or Stack Pane doesn't seem to work either. Suggestions? – Grey Jul 10 '15 at 14:29
  • @Grey, For splitpane it should be root.getItems(). See the splitpane javadoc. – Uluk Biy Jul 13 '15 at 10:57