Posts

Showing posts from June, 2016

Blog overview and remarks.

I have been a java software developer for about three years now.About a year ago i have been using javafx to develop software applications.The amazing thing about javafx it embraces the philosophy in which Java was built upon,which is WORA(write once,run anywhere)this means java can run on any platform and system architecure.Using Javafx you can write amazing software applications for a variety of platforms mobile included; this has been made possible by the Java developer community.The code snippets am going to post on this blog are just but a few tips that may appear confusing to newbies.You can run and test them.If you want to use them in a production environment you notice you will have to optimize the code.You can do that or contact me so that i can optimize the code for you.For questions or interactions mail me at:kelvinkagia@gmail.com.Thanks in advance.

Javafx database driven login script demo.

Image
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Button; import javafx.stage.Stage; import javafx.scene.control.TextField; import javafx.scene.control.PasswordField; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class LoginScript extends Application {     PreparedStatement ps;     Connection conn;     ResultSet rs;     public static

Javafx displaying image from database.

Image
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.control.Button; import javafx.scene.layout.BorderPane; public class PhotoFromDatabase extends Application {     PreparedStatement ps;     Connection conn;     ResultSet rs;           public static void main(String[]args)     {         Application.launch(args);     }     @Override     public void start(Stage primaryStage)     {         BorderPane bp = new BorderPane();         Scene scene = new S

Javafx inserting uploaded image into database.

Image
import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import java.sql.*; import javafx.embed.swing.SwingFXUtils; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.scene.layout.BorderPane; import javafx.scene.control.Button; import javafx.stage.FileChooser; import javax.imageio.ImageIO; public class ImageToDatabase extends Application {      PreparedStatement ps;     Connection conn;     ResultSet rs;                public static void main(

Javafx fetching data from database to a table.

Image
import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.layout.BorderPane; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import javafx.beans.property.SimpleStringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; public class ValuesFromDatabaseToTable extends Application {     PreparedStatement ps;     Connection conn;     ResultSet rs;      String usernameSession;      private final TableView<Contacts> table = new TableView<>();       private final ObservableList<Contacts>data = FXCollections.observableArrayList();     public static void ma

Javafx changing from one scene to another.

import javafx.application.Application; import javafx.scene.layout.StackPane; import javafx.scene.control.Button; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.stage.Stage; public class ChangingScene extends Application {     public static void main(String[]args)     {         Application.launch(args);     }     @Override     public void start(Stage primaryStage)     {         StackPane root = new StackPane();         Scene next = new Scene(root,600,400);//first scene                  Button nextBtn = new Button("next");         root.getChildren().add(nextBtn);//adding nextBtn button on stackpane of scene 2                  //creating scene2         StackPane sp = new StackPane();         Scene back = new Scene(sp,600,400);                  Button backBtn = new Button("back");         sp.getChildren().add(backBtn);                  //adding actions to buttons         ne

Javafx fetching string from a datepicker.

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.layout.BorderPane; import javafx.scene.control.DatePicker; import javafx.scene.layout.FlowPane; import javafx.scene.control.Button; import javafx.scene.control.TextField; public class FetchingFromDatePicker extends Application {     public static void main(String[]args)     {         Application.launch(args);     }     @Override     public void start(Stage primaryStage)     {         BorderPane bp = new BorderPane();         Scene scene = new Scene(bp,600,400);                  //creating the datepicker         DatePicker dateP = new DatePicker();         dateP.setPrefWidth(200);//creating the width of the datepicker         dateP.setPrefHeight(20);//creating the height of the datepicker                  //creating button         Button btn = new B

Javafx adding an icon to a button.

Image
 import javafx.application.Application; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.layout.StackPane; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; public class ButtonGraphicIcon extends Application {     public static void main(String[]args)     {         Application.launch(args);     }     @Override     public void start(Stage primaryStage)     {         StackPane root = new StackPane();         Scene scene = new Scene(root,600,400);         //the camera icon to set on our button         Image cameraIcon = new Image(getClass().getResourceAsStream("camera-128.png"));         ImageView cameraIconView = new ImageView(cameraIcon);         cameraIconView.setFitHeight(15);         cameraIconView.setFitWidth(15);                  //create button         Button btn = new Button("photo");         btn.setGraphic(cameraIconView);//setting icon to button  

Javafx progressbar on finish an action happens(eg enabling a disabled button).

Image
import javafx.application.Application; import javafx.scene.control.ProgressBar; import javafx.scene.control.Button; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.event.ActionEvent; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.layout.BorderPane; import javafx.scene.layout.FlowPane; import javafx.stage.Stage; import javafx.util.Duration; public class ProgressBarActivateBtn extends Application {     public static void main(String[]args)     {         Application.launch(args);     }     @Override    public void start(Stage primaryStage)    {        BorderPane bp = new BorderPane();        Scene scene = new Scene(bp,600,400);                //creating progressbar        ProgressBar pbar = new ProgressBar(0);        pbar.setPrefWidth(400);//setting the width of the progressbar        pbar.setPrefHeight(20);//setting the height of the progressbar                

Javafx changing java icon on stage or replacing it with your own icon.

Image
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; import javafx.scene.layout.StackPane; public class StageIcon extends Application { public static void main(String[]args) { Application.launch(args); } @Override public void start(Stage primaryStage) { StackPane root = new StackPane(); Scene scene = new Scene(root,600,400); //icon for primarystage Image twitterIcon = new Image(getClass().getResourceAsStream("twitter.png"));//twitter like icon primaryStage.getIcons().add(twitterIcon);//adding icon to primaryStage,changing from default java icon to twitterIcon. primaryStage.setScene(scene); primaryStage.setTitle("Primary Stage Iconified"); primaryStage.show(); } } The code above gives the result below:

Javafx adding icons to menu and menuitem.

Image
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Menu; import javafx.scene.control.MenuItem; import javafx.scene.control.MenuBar; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class MenuIcons extends Application {     public static void main(String[]args)     {         Application.launch(args);     }     @Override     public void start(Stage primaryStage)     {                  BorderPane bp = new BorderPane();         Scene scene = new Scene(bp,600,400);                  MenuBar menuB = new MenuBar();//creating our MenuBar         bp.setTop(menuB);//set our menubar at the top of the brderpane                  //creating icons to be used on our menu and menuitems         Image downloadIcon = new Image(getClass().getResourceAsStream("download.png"));         ImageView downloadView = new ImageView(downloadIc