Monday, December 14, 2015

Applying css styles in JavaFx dynamically

We can apply the styles from a css file easily, which are fixed values defined in css, to any element in JavaFx directly by calling getStyleSheets() or getStyleClass().However, think of a scenario where the colours and styles are to be applied dynamically based on the unknown number of choices made available to the user. In this scenario, creating a separate style (or style class) per colour (or style) is practically not possible. However, here is an approach to apply dynamic styles.

With that being said, if you have any other method, I request you to share it and help others to learn more.

Source Code:


public class CssDemo extends Application {
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) {
		primaryStage.setTitle("Hello World!");
		final Button btn = new Button();
		btn.setText("Say 'Hello World'");
		btn.setOnAction(new EventHandler() {
			@Override
			public void handle(ActionEvent event) {
				try {
					String prefix = "background_";
					String suffix = ".css";

					// this temporary file remains after the jvm exits
					File tempFile = File.createTempFile(prefix, suffix);
					// to delete the files created in temp after the application closes
					// deletion happens only if the application exits properly
					tempFile.deleteOnExit();

					String cssContent = ".intro {   -fx-background-color: rgb("
							+ 67 + "," + 98 + "," + 288 + ")}";
					BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
					bw.write(cssContent);
					bw.close();

					String filename = tempFile.getPath();
					btn.getStylesheets().add("file:///" + filename);
					btn.getStyleClass().add("intro");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		StackPane root = new StackPane();
		root.getChildren().add(btn);
		primaryStage.setScene(new Scene(root, 300, 250));
		primaryStage.show();
	}
}

       
 

Including a screenshot of the source code just to make sure it is clear to understand.
 Happy Coding.


Sunday, October 4, 2015

Palindrome Java Code made short

I was asked by a friend to explain a palindrome program in java and I wanted to make it work for both integers and strings. In addition, wanted to make it short and understandable. Checking if a string is a palindrome or not is simple when compared to an integer palindrome. With a string, we can compare the first character with last character, second character with last but one character and so on. In java, we can convert an integer to string using Integer.toString().

In the below we have two methods, with the same name but different types of parameters passed. If an integer is passed parameter, it is converted to string and the same is passed to the method with string argument. Here it is checked if it is a palindrome by checking if it is like a mirror image, with mirror in the middle of the string. If yes, 1 is returned else, -1 is returned. The same value returned by string method is again returned by the integer method.

/**
 * Created by Naveen Kumar on 9/30/2015.
 */
public class PalindromeDemo {
 
public static void main(String args[]){
   PalindromeDemo pd =
new PalindromeDemo();
   
System.out.println((pd.palindromeCheck(343)==1)?"Palindrome":"Not Palindrome");
   
System.out.println((pd.palindromeCheck(3432)==1)?"Palindrome":"Not Palindrome");
   
System.out.println((pd.palindromeCheck("madam")==1)?"Palindrome":"Not Palindrome");
   
System.out.println((pd.palindromeCheck("abcd")==1)?"Palindrome":"Not Palindrome");
  
}

   
public int palindromeCheck(int n){
       
return (new PalindromeDemo()).palindromeCheck(Integer.toString(n));
   
}

   
public int palindromeCheck(String str){
       
for(int i=0;i<(str.length())/2;i++){
            
if(str.charAt(i)!=str.charAt(str.length()-i-1))
               
return -1;
       
}
       
return 1;
   
}
}


Any comments or updates are welcome :)

Happy Coding.