JavaFX: Applying CSS styles to individual elements

This entry is part 44 of 55 in the series JavaFX

We can choose to apply styling to only individual element or control.  We can also apply CSS styling to layout control such as HBox.

We will use the following CSS style sheet to apply some formatting to the Hbox and Label.

.hbox {
    -fx-background-color: lightblue;
    -fx-padding: 20;
    -fx-spacing: 10;
}

.label {
    -fx-font-size: 12pt;
    -fx-font-family: "Cambrias";
}

So now we do not have to use statement such as below to control the padding of the HBox.

hbox.setPadding(new Insets(10, 10, 10, 10));

Here is the full example.

package javafxapplication23;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.scene.layout.Priority;

public class JavaFXApplication23 extends Application {

    @Override
    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        HBox hbox = new HBox(10); // spacing = 10  
        hbox.getStyleClass().add("hbox");

        Label lb = new Label("Send Us:");
        lb.getStyleClass().add("label");
        
        Button button = new Button("Submit");
 
        hbox.setHgrow(button, Priority.ALWAYS);
        button.setMaxWidth(Double.MAX_VALUE);

        hbox.getChildren().addAll(lb, button);

        Scene scene = new Scene(hbox, 300, 250);
        scene.getStylesheets().add("javafxapplication23/style.css");
        stage.setScene(scene);

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

styling

JavaFX: Applying CSS style to a scene

This entry is part 43 of 55 in the series JavaFX

Instead of using the default styles provided by JavaFX, we can create an entirely different look and feel for the application.

For now, we work out a  simple style sheet named style.css that specifies the font to use for the text and the background color and we apply them to the scene instead of individual elements such as the button or the text.

.root
{
    -fx-background-color: lightblue;
    -fx-font-family: "serif";
    -fx-base: #000000;
    -fx-font-size: 24px;
}

As for the code, we apply the CSS style to the scene as shown below.  Note that we are applying the style to the entire application as we are using the .root style class.

package javafxapplication13;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;

public class JavaFXApplication13 extends Application {

    @Override
    public void start(Stage stage) {

        stage.setTitle("JavaFX Program!");

        StackPane stackpane = new StackPane();

        Scene scene = new Scene(stackpane, 500, 300);
        scene.getStylesheets().add("javafxapplication13/style.css");
        
        stage.setScene(scene);

        Label lb = new Label("Stay Calm And Keep Learning");
        Image image = new Image(getClass().getResourceAsStream("cloud.jpg"));
        lb.setGraphic(new ImageView(image));
        lb.setContentDisplay(ContentDisplay.BOTTOM);

        stackpane.getChildren().add(lb);

        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

style

JavaFX: Designing A Button Using CSS

This entry is part 21 of 55 in the series JavaFX

JavaFX supports the standard CSS3 format and this allows us to design JavaFX interface using standard web page design technique.

Let us first create a new file called style.css.

First add a CSS file called style.css as shown in the screenshot below.

css

In our program, we will have to set the scene to use the style sheet using the statement below.

scene.getStylesheets().add("javafxapplication19/style.css");

Once the scene knows the path of the stylesheet, to apply the design to the button, we use a statement similar to below.

btn.getStyleClass().add("button1");
stylesheet
package javafxapplication19;
 
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
 
public class JavaFXApplication19 extends Application {
 
    @Override
    public void start(Stage stage) {
 
        stage.setTitle("JavaFX Program!");
 
        StackPane stackpane = new StackPane();
 
        Scene scene = new Scene(stackpane, 300, 250);
        scene.getStylesheets().add("javafxapplication19/style.css");
        stage.setScene(scene);
 
        Button btn = new Button("Codecrawl");
        btn.getStyleClass().add("button1");
 
        stackpane.getChildren().add(btn);
 
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
 
}

button

Here is the style.css file where we have used to set the font and base color of the button.

.button1 {
    -fx-font: 16 arial; 
    -fx-base: #9CB6CF;    
}

 

 

HTML Table: Formatting all tables and cells the same way

This entry is part 13 of 16 in the series HTML Table

To format all tables and cells the same way, define the attributes in an embedded style sheet.

<!DOCTYPE html>
<html>
<head lang='en'>     
    <meta charset='UTF-8'>
    <title>Codecrawl.com-HTML Table</title>
    <style>
        table {
            border-style: dotted;
            border-color: red;
            border-width: 1px;
        }

        td {
            border-style: dashed;
            border-color: green;
            border-width: 1px
        }

    </style>
</head>
<body>

<table>
    <tr>
        <th>ID</th>
        <th>artiste</th>
        <th>album</th>
    </tr>
    <tr>
        <td>1</td>
        <td>Rod Stewart</td>
        <td>Maggie May</td>
    </tr>
</table>

</body>
</html>

CSS: Applying global style rule to all similar

This entry is part 3 of 26 in the series Basic CSS

Say we have a few unordered lists.  Instead of putting the style in each <ul> tag, we can declare a global style rule for all <ul> tags.

<!DOCTYPE html>
<html>
<head lang='en'>
    <meta charset='UTF-8'>
    <title>Lists</title>

    <style type='text/css'>
        ul {
            list-style-type: square;
        }
    </style>

</head>
<body>

<h4>American Rock</h4>
<ul>
    <li>Linkin Park</li>
    <li>Nirvana</li>
    <li>Metalica</li>
</ul>

</body>
</html>