How can I modify the color of JavaFX text using CSS?

Looking to jazz up my JavaFX application with some custom CSS styling.

Managed to link a stylesheet to my app using the following code:

//Java code
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/path/to/fxml"));
        Scene scene = new Scene(loader.load());
        
        scene.getStylesheets().add(getClass().getResource("/path/to/css").toExternalForm());

Trying to change the text color, but can't find a default style class for text objects here.

Designed FXML files through SceneBuilder. Assigned a text object to a class by entering the class name in the appropriate text field (Sidebar >> JavaFX CSS >> Style Class).

https://i.stack.imgur.com/0dIUw.png

CSS file snippet:

.myText {
    -fx-text-fill: #ffffff;
}

However, applying this method doesn't seem to have any effect when running the app. Any suggestions on how to tackle this issue?

Answer №1

for instance, consider the following scenario:

<Button fx:id="stop" layoutX="100.0"
     layoutY="14.0" mnemonicParsing="false"
     onAction="#onActionStopBtn"
     style=" -fx-background-color:
                    #000000, 
                    linear-gradient(#7ebcea, #2f4b8f),
                    linear-gradient(#426ab7, #263e75),
                    linear-gradient(#395cab, #223768);
             -fx-background-insets: 0,1,2,3;
             -fx-background-radius: 3,2,2,2; 
             -fx-padding: 12 30 12 30;
             -fx-text-fill: white;
             -fx-font-size: 12px;"
     text="Stop" />

introduce certain styling to the FXML Button
refer to this resource for more information

Answer №2

At last, I managed to uncover the source of the issue. It turns out that adding a stylesheet to FXML in SceneBuilder was the solution.

Subsequently, everything began functioning properly.


Interestingly enough, I am puzzled as to why I had to incorporate the stylesheet in SB when I had already included it in my code beforehand. Could this possibly be a glitch? Or perhaps I'm just being foolish?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I remove the border of an image hyperlink using CSS?

Let's consider the following situation: I have defined the CSS rule a { color: Red } When using Internet Explorer, if I have: <a href="..."> <img src='...' /> </a> it results in a red border around the image. How can ...

Encountering a null pointer exception when utilizing the Selenium page factory

Whenever I try to use Selenium Page Factory to identify objects, I keep encountering a null pointer exception. It's puzzling because the Page Factory works perfectly in my first Java file test case, but as soon as I try using the same Page Factory in ...

The waveform's bottom appears distorted when using Bootstrap

I'm currently experimenting with creating a header wave in Bootstrap CSS using SVG shape. However, I encountered an issue when implementing bootstrap into the HTML code - my design resembles the second image instead of the desired look of the first on ...

Enable the option to customize the icon of the recently activated sidebar menu item

I need help with changing the arrow icon of a menu-item only when it is clicked, without affecting all other menu-items. In the image below, you can see that currently, clicking on any menu-item changes all the arrows. Attached Image //sidebarMenu jQu ...

Decrease the size of the "subscribe" button

Our "subscribe" button is currently displayed like this: https://i.sstatic.net/toiOb.png However, we are looking to reduce the extra width and have it appear more like this: https://i.sstatic.net/NaaYJ.png We are using the same code for both versions: ...

Error encountered: java.net.SocketException - Connection reset when executing driver.close() or driver.quit() in Selenium

Whenever I try using the driver.close() or driver.quit() statement, I consistently encounter a java.net.SocketException: Connection reset. Despite this exception, my tests continue to run smoothly without any interruption. However, I am curious to understa ...

How can I add a loading page to my HTML/CSS website?

Currently, I am delving into the realm of coding and have encountered an issue while trying to integrate a loading page into my HTML. Despite referencing the code from this source, it appears that the loading page is not functioning as intended. Any assist ...

Dynamic expand/collapse animation in React with adjustable height feature

I'm currently working on an Expand component and attempting to implement an expand animation upon toggle without success. I want this animation to be dynamic without explicitly setting the element's height: export const Expand = ({ startOpen, con ...

How to customize the color of the clock symbol in a MUI TextField set to type 'time'

<TextField id="endTime" label="End Time" onChange={onEndTimeChange} type="time" defaultValue="16:00" className={classes.textField} /> By using the attribute 'type="time"', an ic ...

Is sketching on a canvas with a personalized technique on your mind?

I'm currently working on developing a 2D game for Android, and I've run into some issues that I could use some help with. Within my package, I have a thread that handles drawing on a canvas. The challenge I'm facing is trying to allow insta ...

The class in my CSS is currently impacting all the menu bar items, but I specifically want it to only affect my photo gallery

<div class="gallery"> <a tabindex="1"><img src="images/smile.jpg"></a> <a tabindex="1"><img src="images/smile.jpg"></a> <a tabindex="1"><img src="images/smile.jpg"></a> ...

Is there any trace of the specific text present on the page in JUnit/Hamcrest?

Currently, I'm leveraging Junit and Selenium WebDriver to identify misspelled words from a predefined word bank on a particular website. Additionally, my testing involves Hamcrest which is now integrated with Junit. @Test public void filter() throws ...

Create two grid components for Material-UI and use Flexbox to ensure that both items have equal height

I'm currently working on ensuring that two grid items have the same height using flex in Material UI, but I've hit a roadblock. It seems like my approach with the Grid element might be incorrect. I've experimented with adjusting the height a ...

What is the purpose of Hibernate attempting to connect to hibernate_sequence while utilizing GenerationType.IDENTITY strategy?

My current project involves the use of MySQL along with Hibernate and JPA. I have ensured that all my entities are properly annotated with @ID @GeneratedValue(strategy = GenerationType.IDENTITY). However, I am facing an issue where Hibernate attempts to s ...

The Parent Div is styled with a background color, and I would like the sub div to inherit the background from whatever is behind the

I have a website at . When you click on a blue tab (except the middle left one), a larger tab appears with two Divs inside. What I am trying to achieve is having the sub divs show what's behind the green Div, especially while it's in motion. This ...

Do you need to align images side by side and have clickable images positioned beneath each one?

After searching high and low, I couldn't find a similar question. My goal is to transform this from a vertical layout to a horizontal one: https://i.stack.imgur.com/4kZNM.png Check out the code snippet below: ghost<br class="newline"> <img ...

In CSS, use the p tag to make sure content fills the available space when the browser is resized beyond a certain point

Before I begin, I must clarify that I do not specialize in front-end development and my expertise in UI/UX is limited. With that said, here is my query: I have a div containing p tags. I wish for this div to have a width: 700px when the browser window is ...

Ways to incorporate a dictionary into your website's content

I am in the process of developing a website for educational purposes while also honing my web programming skills. On this website, I have encountered some complicated terms that may be difficult for users to understand, so I want to implement a tooltip/mod ...

I am looking for a connection string that can be used with Parasoft tool or Java to connect to a Mongo Db database with LDAP authentication

Attempted using MongoClient for connecting without authentication with the following connection string, but it does not work for LDAP authentication. MongoClient client = new MongoClient("mongodb://username:pass@localhost/portnumber") Is there a specific ...

Continuously querying the database in JSP to automatically fetch data

I've been exploring Spring recently and I'm curious if there is a way to set up automatic database polling using Spring 4. Ideally, I'd like the code to run without requiring the user to manually refresh any pages. Is it possible for my jsp ...