This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Button to switch theme  RSS feed

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm a student who needs to make the game 'PacMan' for a school project. I am starting with a main menu which contains a few settings. I'm currently stuck at changing the themes of the game. I have added a classic and christmas theme and I would like to change these themes using 2 buttons 'classic' & 'xmas'.
For some reason I can't make it work. Can anyone please help? This is my code:

FXMLSideMenuController:


SideMenu.java:


Thank you in advance!

 
Master Rancher
Posts: 1107
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't really looked at your code but if you wanna switch scenes on a buttin click or whatever, set a onAction on your button and in you rmethod do:


for this to work you do have to make a static Stage variable in your mainClass and set it to your primaryStage.
An alternative could be to set window to a new Stage.
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:I haven't really looked at your code but if you wanna switch scenes on a buttin click or whatever, set a onAction on your button and in you rmethod do:


for this to work you do have to make a static Stage variable in your mainClass and set it to your primaryStage.
An alternative could be to set window to a new Stage.



I just tried this and this doesn't work. I should also tell you I am using .css files to change the themes. These are the files:

classic.css


xmas.css
 
Daniel Demesmaecker
Master Rancher
Posts: 1107
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're dutch?
So you're not trying to change the scenes, your trying to change the cssfile loaded...?
in that case add this before your window.show() call:
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:You're dutch?
So you're not trying to change the scenes, your trying to change the cssfile loaded...?


Yes I am Dutch.
I'm just trying to 'change the theme' and I figured the best way to do this is via css files.. This might not be the case, I'm not sure.
So now I basically have this 'classic theme' and 'christmas theme' in .css files which I would like to 'load' into the application on a button click. Meaning the whole layout of the application changes to the theme selected and also stays this way until the other button is clicked. Please note that this is just the menu, meaning there are multiple panes and etc.
 
Daniel Demesmaecker
Master Rancher
Posts: 1107
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in that case add this before your window.show() call:



I never use cssFiles in javaFx, I do my design in scenebuilder, but for this case I guess it would make more sence to change the css, otherwise you would have to make to fxmlfile
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:in that case add this before your window.show() call:



I never use cssFiles in javaFx, I do my design in scenebuilder, but for this case I guess it would make more sence to change the css, otherwise you would have to make to fxmlfile


So the code I added to the controller looks like this:


and the main looks like this:


But it still doesn't work. It says: "incompatible files: FXMLLoader cannot be converted to Parent".
 
Sheriff
Posts: 5690
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 I believe this needs to be either
or
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:  I believe this needs to be either
or


Doesn't change anything!
 
Daniel Demesmaecker
Master Rancher
Posts: 1107
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute is right, if you look at my example you see I casted to parent


I also believe you don't have to include the resources folder, so if you didn't use any subfolders in your resourceroot it would be just style.css
 
Knute Snortum
Sheriff
Posts: 5690
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Axel Rey wrote:

Knute Snortum wrote:  I believe this needs to be either
or


Doesn't change anything!


You get the exact same error message?  What is the root of the FXML?
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Knute is right, if you look at my example you see I casted to parent


I also believe you don't have to include the resources folder, so if you didn't use any subfolders in your resourceroot it would be just style.css


I have to include the Resources folder as I created it in my application and put the .css in there. I still get the same error message, my root is just the default one which is created once you open a new Netbeans app project.
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Managed to fix the error by using Knutes' reply, but now when trying to run this part of the code gives an error:


"cannot find symbol. Symbol: method getStylesheets(), location: variable window of type Stage"
 
Daniel Demesmaecker
Master Rancher
Posts: 1107
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this as sampleCode...

The controller:


Your MainClass:


The fxml is anchorpane with nothing other then a button.
The cssfiles are the same as you use, I just added a one line to change the bgcolor in black or pink to demonstrate it works...

 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Use this as sampleCode...

The controller:


Your MainClass:


The fxml is anchorpane with nothing other then a button.
The cssfiles are the same as you use, I just added a one line to change the bgcolor in black or pink to demonstrate it works...


Thank you! This really made things clear and now my buttons are working. There's only one more thing: when I click on the button, my button styles swap but my panes don't as you can see on the pictures below. My christmas panes should also change colours, how can I fix this?
Classic:


Xmas:


 
Daniel Demesmaecker
Master Rancher
Posts: 1107
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you have to asign/use the right id to the element you want to change
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:you have to asign/use the right id to the element you want to change


I have done that (as you can see in the .css files above). I just think there's an issue with my FXML which overrides these panes or something.
 
Daniel Demesmaecker
Master Rancher
Posts: 1107
18
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your fxml and in your css >
 
Axel Rey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Axel Rey wrote:

Daniel Demesmaecker wrote:you have to asign/use the right id to the element you want to change


I have done that (as you can see in the .css files above). I just think there's an issue with my FXML which overrides these panes or something.


Found what was wrong: I had a set background color for the panes so they would never be able to be overriden. I've now fixed this.

Thanks for your help guys, this really worked!
 
Knute Snortum
Sheriff
Posts: 5690
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad we could be of help!
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!