Style java with css

Use CSS in Java Applications

The syntax is given as follows; Defining Styles in StyleSheet A style definition can be given by using the name of the style which is also known as selecter and series of the rules that set the properties for the styles. Style class selector names are preceded by a dot(.)

Use CSS in Java Applications

I am working with Java for quite some time now and now I wanted to make a new Application and I was wondering, if it would be possible to make Java look more modern with new buttons and things like this.

So I started searching for solutions to apply CSS (used in web-programming) to Swing components but didn’t find a real solution.

I dont want to use a lot of not build-in options, so using Frameworks and Libraries should be my last option.

My question is: Is there a way to apply CSS to my Java UI? And if it’s not possible: is there another way of making the whole GUI look different by adding some kind of style-sheet to it?

You should look up javafx. You can attach a CSS file to a GUI. it is fairly easy to use. I can give you some example code if you want.

These are some good tutorials on how to use it: https://www.youtube.com/watch?v=FLkOX4Eez6o

Читайте также:  Eclipse and java version

I use eclipse to create a css file right click the project -> select new -> other -> CSS file

import javafx.application.*; import java.text.*; import java.util.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.input.*; import javafx.stage.*; import javafx.geometry.*; public class Display extends Application < public static void main(String[] args)< launch(args); >@Override public void start(Stage stage) throws Exception < HBox root = new HBox(); Button button = new Button("button"); Label label = new Label("Label"); root.getChildren().addAll(button, label); Scene scene = new Scene(root, 700,300); scene.getStylesheets().add("Style.css"); stage.setScene(scene); stage.setTitle("Title"); stage.show(); >> 
 .root < -fx-background-color: linear-gradient(#383838, #838383); -fx-font-size: 11pt; >.label < -fx-text-fill: #e8e8e8; >.button < -fx-background-color: linear-gradient(#dc9556, #ab4642); -fx-background-radius: 100; >.text-area

What you want is javafx. It allows you to develop an app for desktop and web as well, but you can style it using CSS.

If you are looking to add CSS to your UI (and you haven’t already started), I would take a look at javaFX, which would allow you to do just that.

Here is an Oracle tutorial on styling the UI with CSS:
http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/apply-css.htm#CHDGHCDG

Change Css for All elements from JS

I’m trying to change jQuery dialog’s font-family from a little display settings panel. I’ve tried all of these to no avail:

$('#orderHolder').style.fontFamily='Bookman Old Style'; $('.ui-dialog-content').style.fontFamily='Bookman Old Style'; $('.ui-widget-content').style.fontFamily='Bookman Old Style'; $('#orderHolder').css("font-family", "Bookman Old Style'; $('*').css('font-family', 'Bookman Old Style'); //this is what I'm after 

(#orderHolder being the div that contains the dialog content)

in the .css file works fine, and the overall effect is what I am after.

So, How do you select all elements for styling in javascript?

$('*').css('font-family','Bookman Old Style'); 
document.querySelectorAll('*').style.fontFamily = 'Bookman Old Style'; 

Actually you need to iterate over all element when you use querySelectorAll:

var el = document.querySelectorAll('*'); for(var i=0;i

But this is unnecessary to iterate over all element while using asterisks selector, you can just use querySelector:

document.querySelector('*').style.fontFamily='Bookman Old Style'; 

But actually if you set font-family to the body your font will be inherited to all element, so rather than other thing just apply to the body.

You may need to make it as important style: Also if any other font-family is there then please remove it. try the below code

$('#content').find('*').css("font-family","'Bookman Old Style'"); 

in the .css file works fine, and the overall effect is what I am after.

In that case, since you apparently aren’t applying font-family to more specific selectors, your best bet is to add a style element to the document with that rule:

. if they all inherit their style from body .

That way, it applies throughout, and also to new elements you add afterward.

None of the previous answers worked for me. But this:

document.head.innerHTML = document.head.innerHTML + '* ' 

Javas set Code Example, Give the output of the following code: int a = 10; int b = 20; String s = Integer.toString (a); String t = Integer.toString (b); System.out.println ( (s+t)); java …

JavaFX CSS

What is CSS?

CSS (Cascading Style Sheets) is a design language which is used to enhance the appearance of the web pages without changing its functionality. It only deals with the way, a web page is presented on the web browser.

Using CSS, we can define the color, size, font styles, spacing between the paragraph, alignment and many more thing for a web page so that it can look more precise and better. We can also configure the background of the application, layout, design and variety of display for the different size devises.

CSS in JavaFX

JavaFX, being the new generation UI library, provides the facility to configure the theme of the application. JavaFX provides the package javafx.css which contains all the classes to apply the CSS to the JavaFX application.

Applying CSS to the JavaFX application is similar to applying CSS to the HTML page. In this part of the tutorial, we will discuss styling rules and the steps to invoke them in JavaFX.

Default Style Sheet

JavaFX uses caspian.css as the default CSS file. It is found in JavaFX Run time JAR file, jfxrt.jar . This style sheet defines the default style rules for the root node and UI controls. This file is located at the path /jre/lib under the JDK installation directory. The following command can be used to extract the style sheet from the JAR file.

# jar xf jfxrt.jar # com/sun/javafx/scene/control/skin/caspian/caspian.css

Adding Style-sheets to scene

However, JavaFX provides us the facility to override the default style sheet and define our own styles for every node of the application. The Style-sheet we create, must have the extension .css and it must be located in the directory where the main class of the application resides.

In JavaFX,there is a specific syntax of applying CSS to the scene. The syntax is given as follows;

Scene scene = new Scene(root,500,400); scene.getStylesheet().add("path/Stylesheet.css");

Defining Styles in StyleSheet

A style definition can be given by using the name of the style which is also known as selecter and series of the rules that set the properties for the styles. Styling rules are given within the braces. Consider the following example named as mystyle.css . It defines the style definition for the each button node used in its container application.

Example

Selectors

There are various types of styles used in JavaFX. However, each type considers its own conventions regarding selectors. Style class selectors naming conventions are,

  1. If the style class selector consists of more than one word, use the hyphen between them.
  2. Style class selector names are preceded by a dot(.)

Examples of the selectors are:

the style for a particular node can be defined by using the node’s ID. This ID can be set using setId() method. Use the # symbol before the Node_ID to make a style name for that node. For example, the node having id my_label can have the following type of selector name.

Defining Rules in Style-sheets

The rules for a style definition assigns values to the properties. There are some conventions for the property names that are given as follows.

  1. If the property name consists of more than one word then use hyphen (-) to seperate them.
  2. Property name for the styles are preceded by -fx-.
  3. Property name and the value are seperated by colon (:).
  4. Rules are seperated by the semicolon (;).

the example of defining rules for the properties is as follows.

-fx-background-color : #333356; -fx-font : 16px "serief";

There is a special style class named as .root defined in javafx. It is applied to the root node of the scene object. Since all the nodes of the application are the children of the root node therefore the style rules applied to this class can be applied to the whole scene graph of the application.

Class Styles

The class styles can be created by adding its definition to our style sheet. For example;

to add the above mentioned style class to the appropriate node, use the method getStyleClass().add() sequence of methods.

Button button = new Button("SUBMIT"); button.getStyleClass().add(button1);
Example:
package application; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class JavaFX_CSSExample extends Application < @Override public void start(Stage primaryStage) throws Exception < Label first_name=new Label("First Name"); Label last_name=new Label("Last Name"); TextField tf1=new TextField(); TextField tf2=new TextField(); Button Submit=new Button ("Submit"); GridPane root=new GridPane(); root.setHgap(10); root.setVgap(15); Scene scene = new Scene(root,400,200); root.addRow(0, first_name,tf1); root.addRow(1, last_name,tf2); root.addRow(2, Submit); //Adding CSS file to the root root.getStylesheets().add("Style.css"); primaryStage.setScene(scene); primaryStage.show(); >public static void main(String[] args) < launch(args); >> // style.css .label < -fx-background-color:Black; -fx-text-fill:white; -fx-font-size:16; -fx-border-color: Wheat; -fx-border-radius: 5; >.button < -fx-background-color:Black; -fx-font-family:courier_new; -fx-text-fill:White; -fx-border-color: Wheat; -fx-border-radius: 5; -fx-font-size:16;

JavaFX CSS

ID Styles

JavaFX provides us the facility to create the style for the individual node. The style name can be given as the ID name preceded by the hash(#) symbol.

Example

package application; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.scene.text.Text; import javafx.stage.Stage; public class SignUP extends Application < @Override public void start(Stage primaryStage) throws Exception < //Adding Labels to the form Label first_name = new Label("First Name"); Label last_name = new Label("Last Name"); Label Email = new Label("Email ID"); Label Password = new Label("Password"); //Adding text-field to the form TextField tf1=new TextField(); TextField tf2=new TextField(); TextField tf3=new TextField(); TextField tf4=new TextField(); //creating submit button Button Submit=new Button ("Submit"); //setting ID for the submit button so that the particular style rules can be applied to it. Submit.setId("submit"); //Creating reset button Button Reset=new Button ("Reset"); //Creating title Text title = new Text(); title.setText("Sign Up"); title.setX(120); title.setY(120); title.setUnderline(true); title.setId("title"); //creating grid-pane GridPane root=new GridPane(); //creating Scene object Scene scene = new Scene(root,400,400); //adding the the nodes to the GridPane's rows root.addRow(0, title); root.addRow(2, first_name,tf1); root.addRow(3, last_name,tf2); root.addRow(4, Email,tf3); root.addRow(5, Password,tf4); root.addRow(7, Submit,Reset); //setting horizontal and vertical gaps between the rows root.setHgap(10); root.setVgap(10); //adding style-sheet to the Scene root.getStylesheets().add("form.css"); primaryStage.setScene(scene); primaryStage.show(); >public static void main(String[] args) < launch(args); >> //form.css .root < -fx-background-color:Wheat; -fx-alignment:center; -fx-background-radius: 100; >.label < -fx-font-size:18pt; -fx-font:bold 10pt"Arial"; -fx-padding:10 >.button < -fx-effect:dropshadow(one-pass-box,black,8,0.0,2,0); -fx-border-radius:20; -fx-font:bold10pt"Arial"; >#title

JavaFX CSS

Setting Inline styles

JavaFX facilitates us to define the CSS rules in the JavaFX application code itself. However, the rules defined in the JavaFX application code is given precedence over styles from the style sheet.

In the following example, we have defined the CSS rules in the code file itself which shows the way by which the color and the font of a label can be changed using CSS rules.

Label label1 = new Label("Name: "); label1.setStyle("-fx-background-color : blue, -fx-text-fill : white");
Example
package application; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class JavaFX_CSSExample extends Application < @Override public void start(Stage primaryStage) throws Exception < Label first_name=new Label("First Name"); // setting style for the label first_name first_name.setStyle("-fx-background-color:Black; -fx-text-fill:white; -fx-font-size:16"); Label last_name=new Label("Last Name"); //setting style for the label last name last_name.setStyle("-fx-background-color:Black; -fx-text-fill:white;-fx-font-size:16"); TextField tf1=new TextField(); //setting style for the first text field tf1.setStyle("-fx-background-color:Wheat; -fx-text-fill:Black; -fx-font-size:16"); TextField tf2=new TextField(); //setting style for the second TextField tf2.setStyle("-fx-background-color:Wheat; -fx-text-fill:Black; -fx-font-size:16"); Button Submit=new Button ("Submit"); //setting styles for the button Submit.setStyle("-fx-background-color:Black; -fx-font-family:courier_new; -fx-text-fill:white;-fx-font-size:16"); GridPane root=new GridPane(); root.setHgap(10); root.setVgap(15); Scene scene = new Scene(root,400,200); root.addRow(0, first_name,tf1); root.addRow(1, last_name,tf2); root.addRow(2, Submit); primaryStage.setScene(scene); primaryStage.show(); >public static void main(String[] args) < launch(args); >>

JavaFX CSS

JavaFX CSS - javatpoint, JavaFX uses caspian.css as the default CSS file. It is found in JavaFX Run time JAR file, jfxrt.jar. This style sheet defines the default style rules for the root node and …

Источник

Оцените статью