Showing posts with label java Fx. Show all posts
Showing posts with label java Fx. Show all posts

Saturday, 9 January 2016

Tutorial On Toggle Button In JavaFx

In this article we are going to show how to create a toggle button in java-fx with source code.Previously we have also created toggle button in java swing.
See Related Tutorials:
 how to create toggle button in javaswing.
How To Create HelloWord Application In JavaFx Using Netbeans.
Combo box in java-fx
Cosmetic Planet

CODE:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class mybej extends Application
{

@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("Toggle Button In JavaFx");

final ToggleGroup group = new ToggleGroup();
final ToggleButton tb1 = new ToggleButton("ON");
tb1.setToggleGroup(group);
tb1.setSelected(false);
final Rectangle rect = new Rectangle(145, 50);
rect.setFill(Color.RED);
rect.setLayoutX(40);
rect.setLayoutY(40);
g.getChildren().addAll(tb1,rect);
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
public void changed(ObservableValue<? extends Toggle> ov,
Toggle toggle, Toggle new_toggle) {
if (new_toggle == null)
{
rect.setFill(Color.RED);
tb1.setText("ON");
}
else
{
rect.setFill(Color.BLUE);
tb1.setText("OFF");
}
}
});

Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}

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

}

OUTPUT:

Friday, 1 January 2016

,

Tic Tac Toe Game In Java-Fx For Download

Tic tac toe is one of the most basic games in world of gaming.The reason of sharing this game code is to provide a opportunity to new game developers to learn basics of gaming in java-fx.This game is to free to download so grab it and don't forget share your reviews about it.

SNAP SHOT:








Wednesday, 4 November 2015

Circle Shape Stage In JavaFx

In this code I show you how can we use javafx graphics to create stage of custom shapes.In this program I have create an stage of circle shape you can create stage of any shape by making change in this line. 

Circle dragger = new Circle(100, 100, 100,Color.CADETBLUE);

Enjoy reading.

CODE:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class mybej extends Application {
Double intx,inty;
@Override
public void start(final Stage stage) {

stage.initStyle(StageStyle.TRANSPARENT);
Text text = new Text("Transparent STAGE");
Circle dragger = new Circle(100, 100, 100,Color.CADETBLUE);
text.setFont(new Font(40));
Group g = new Group();
Button min = new Button("-");
min.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.setIconified(true);
}
});
Button close = new Button("X");
close.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
stage.close();
}
});

dragger.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
intx = (me.getScreenX() - stage.getX());
inty = me.getScreenY() - stage.getY();
}
});

//when screen is dragged, translate it accordingly
dragger.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
stage.setX(me.getScreenX() - intx);
stage.setY(me.getScreenY() - inty);
}
});
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setPadding(new Insets(25,0,0,60));
hBox.setAlignment(Pos.TOP_CENTER);
hBox.getChildren().addAll(min,close);

g.getChildren().addAll(dragger,hBox);
final Scene scene = new Scene(g,300, 250);
scene.setFill(Color.TRANSPARENT);
stage.setScene(scene);
stage.show();

}

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

OUTPUT:

Round shape stage in javafx

Wednesday, 21 October 2015

,

How To Open And Save File In Java Fx

Before reading this article you should read Tutorial on file chooser in javafx,Tutorial on context menu in javafx. In this tutorial we will see how to use file chooser to open and save  image files in java fx.following are the explanations of methods used in these tutorials.

imageIo.write(RenderedImage im,String format,File outputlocation)

Writes an image using an  ImageWriter that supports the given specified format of a image. If there is already a File present, its will be over written.
im: Name of image that is to be written.

format: In this parameter you have to specify format in which you want to write a image.

outputlocation: Here you have to specify the location of image where you have to write.

HOW TO SAVE IMAGE FROM JAVAFX APPLICATION USING FILE CHOOSER:

import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class mybej extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start( final Stage primaryStage) {
final Group root = new Group();
final Image pic = new Image ("fac.jpg");
ImageView iv = new ImageView();
iv.setImage(pic);

final FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save File Chooser");
final Scene scene = new Scene(root, 600, 350,Color.WHITE);
final ContextMenu cm = new ContextMenu(); 
MenuItem cmItem2 = new MenuItem("Save Image");
cm.getItems().add(cmItem2);
cmItem2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {

fileChooser.setTitle("Save Image");

File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
try {
ImageIO.write(SwingFXUtils.fromFXImage(pic, null), "png", file);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
if (e.getButton() == MouseButton.SECONDARY)  
cm.show(root, e.getX(),e.getY());
}
}); 
primaryStage.setScene(scene);
root.getChildren().addAll(iv);
primaryStage.show();
}

}

OUTPUT:



HOW TO OPEN IMAGE FILE USING FILE CHOOSER IN JAVA FX:


import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public final class mybej extends Application {

private Desktop desktop = Desktop.getDesktop();

@Override
public void start(final Stage stage) {
stage.setTitle("File Chooser Sample");
final FileChooser fileChooser = new FileChooser();
File file = fileChooser.showOpenDialog(stage);

if (file != null) {
openFile(file);
}


Group root = new Group();

stage.setScene(new Scene(root));

}

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

private void openFile(File file) {
try {
desktop.open(file);
} catch (IOException ex) {
System.out.print(ex);
}
}
}

How To Create Musical Piano In JavaFx

 After using lots of application based on audio I get motivated to create a program based on audio,this program used AudioClip class to play a music when button of piano is pressed and I have used play method to play particular audioclip when piano button is pressed following code show how to create a musical piano in javafx.

CODING:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.media.AudioClip;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Mypiano extends Application
{

@Override
public void start(Stage primaryStage)
{

Group root = new Group();  
Rectangle r = new Rectangle(300,60,60,200);
r.setFill(Color.WHITE);
r.setStroke(Color.BLACK);
Rectangle ra = new Rectangle(340,60,50,130);
ra.setFill(Color.BLACK);
Rectangle r1 = new Rectangle(361,60,60,200);
r1.setFill(Color.WHITE);
r1.setStroke(Color.BLACK);
Rectangle r1a = new Rectangle(400,60,50,130);
r1a.setFill(Color.BLACK);
Rectangle r2 = new Rectangle(421,60,60,200);
r2.setFill(Color.WHITE);
r2.setStroke(Color.BLACK);
Rectangle r2a = new Rectangle(461,60,50,130);
r2a.setFill(Color.BLACK);
Rectangle r3 = new Rectangle(483,60,60,200);
r3.setFill(Color.WHITE);
r3.setStroke(Color.BLACK);
Rectangle r3a = new Rectangle(524,60,20,130);
r3a.setFill(Color.BLACK);
Rectangle r4 = new Rectangle(545,60,60,200);
r4.setFill(Color.WHITE);
r4.setStroke(Color.BLACK);
Rectangle r4a = new Rectangle(584,60,20,130);
r4a.setFill(Color.BLACK);

final AudioClip note1 =
new AudioClip(getClass().getResource("Note1.wav").toString());
final AudioClip note2 =
new AudioClip(getClass().getResource("Note2.wav").toString());
final AudioClip note3 =
new AudioClip(getClass().getResource("Note3.wav").toString());

final AudioClip note4 =
new AudioClip(getClass().getResource("Note4.wav").toString());


final AudioClip note5 =
new AudioClip(getClass().getResource("Note5.wav").toString());


final AudioClip note6 =
new AudioClip(getClass().getResource("Note6.wav").toString());

final AudioClip note7 =
new AudioClip(getClass().getResource("Note7.wav").toString());

final AudioClip note8 =
new AudioClip(getClass().getResource("Note8.wav").toString());
r.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note1.play();

}
});
r1.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note2.play();

}
});
r2.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note3.play();

}
});
r3.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note4.play();

}
});
r4.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note5.play();

}
});
ra.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note6.play();

}
});

r1a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note7.play();

}
});
r2a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note1.play();

}
});
r3a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note2.play();

}
});
r4a.setOnMouseClicked(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent me) {

note8.play();

}
});


root.getChildren().addAll(r,ra,r1,r1a,r2,r2a,r3,r3a,r4,r4a);
Scene scene = new Scene(root, 600, 300);
primaryStage.setTitle("PIANO");
primaryStage.setScene(scene);
primaryStage.show();
}

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

}

OUTPUT:


Tutorial On Button In Java Fx

In this article I have tried to show you how to use button in javafx application button in javafx can be used in three different modes.
  1. NORMAL:Creates a simple push button.
  2. DEFAULT:Creates a simple button that can be operated by enter key.
  3. CANCEL:Creates a button that can be operated by escape button.
In this program we have shown you how to create default button.setLayoutX,setLayoutX is used to position of button in screen.

CODING:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;


public class mainfx extends Application 
{

@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("FX Button");
Button btn = new Button("Press To Exit");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);

btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(1);
}
});

g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}


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

}

APPLYING MOUSE EVENT IN BUTTON:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class mainfx extends Application {

Button btn = new Button();
@Override
public void start(Stage primaryStage) {
Group g = new Group();
primaryStage.setTitle("Button Event In JavaFx");
btn = new Button("Enter Mouse To Rotate");
btn.setMinWidth(100);
btn.setLayoutX(100);
btn.setLayoutY(100);

btn.addEventHandler(MouseEvent.MOUSE_ENTERED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
btn.getTransforms().add(new Rotate(45));
}
});    

g.getChildren().add(btn);
Scene sc = new Scene(g,400,300);
primaryStage.setScene(sc);
primaryStage.show();
}


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

}









Monday, 19 October 2015

Tutorial On File Chooser In Java Fx

In our previous article we have also discussed about jfilechooser the reason of writing again an article on filechooser in javafx is that in javafx it has some new features that we are going to   discussed in this article.

SAVE FILE CHOOSER IN JAVAFX:

Below program shows how to create a simple save file chooser this used when want to save a particular file using filechooser. 

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class mybej extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start( Stage primaryStage) {
Group root = new Group();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Save File Chooser");
fileChooser.showSaveDialog(primaryStage);
primaryStage.setScene(new Scene(root, 500, 400));

}

}


OPEN FILE CHOOSER IN JAVAFX:

This program can be used to create a open file chooser that can be useful to open a particular file.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class mybej extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start( Stage primaryStage) {
Group root = new Group();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open File Chooser");
fileChooser.showOpenDialog(primaryStage);
primaryStage.setScene(new Scene(root, 500, 400));

}

}



HOW TO SET INITIAL DIRECTORY OF FILE CHOOSER IN JAVAFX:

JavaFx provides a new option by using this option we can set the initial directory of filechooser.This can be done by simple adding below code in your code. 
fileChooser.setInitialDirectory(new File("C:"));

HOW TO ADD EXTENSION FILTER  IN FILECHOOSER IN JAVAFX:

Some we want that only a particular type of file is should be shown by file chooser in javafx we can do this by simply adding this code in this program we have added jpg extion filter you can change it according to your need.
 fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("JPG", "*.jpg"),
new FileChooser.ExtensionFilter("PNG", "*.png"),
new FileChooser.ExtensionFilter("JAVA", "*.java")    
);




How To Create Pop-Up Menu In JavaFx

In this tutorial we show how to create pop up menu in javafx using context menu.Context menu can be created using ObservableList of menu items user have option to add any of menu like radio menu and checkmenu.For creating pop-up menu we have to first create a context menu then we have to add a menuitemin in context menu accoding to our need following program shows how to create a pop-up menu.

CODING:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class mybej extends Application 
{

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

@Override
public void start(Stage primaryStage) 
{
final Group root = new Group();
final ContextMenu cm = new ContextMenu();
MenuItem menu1 = new MenuItem("First Menu");
Menu menu = new Menu("Second Menu ");
MenuItem submenu = new MenuItem("First Sub Menu");
MenuItem submenu1 = new MenuItem("Second Sub Menu");
MenuItem menu3 = new MenuItem("Third Menu");
cm.getItems().add(menu1);
cm.getItems().add(menu);
menu.getItems().add(submenu);
menu.getItems().add(submenu1);
cm.getItems().add(menu3);
Scene scene = new Scene(root, 600, 350,Color.WHITE);
scene.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
if (e.getButton() == MouseButton.SECONDARY)  
cm.show(root, e.getX(),e.getY());
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("POP MENU IN JAVAFX");
primaryStage.setWidth(200);
primaryStage.setHeight(200);
primaryStage.show();
}
}

OUTPUT:


Saturday, 17 October 2015

,

How To Load And Refresh Data Base Data In Table JavaFx

In this tutorial we are going to discussed how to load database data in table after the table have loaded sometimes data becomes change we should have a option to refresh our data in this program we have show you how to refresh table in javafx on a click.This enable user to continuously update database data.  

CODING:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
public class mybej extends Application 
{

private final TableView<myData> table = new TableView<>();
private final ObservableList<myData> data =
FXCollections.observableArrayList();
String s,s1,s2,s3,s4,s5;
Button b1 = new Button("REFRESH");
ResultSet database,database1;
Statement st,st1;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
TableColumn firstCol = new TableColumn("First Name");
firstCol.setMinWidth(100);
firstCol.setCellValueFactory(
new PropertyValueFactory<>("name"));
TableColumn secCol = new TableColumn("Fax-Number");
secCol.setCellValueFactory(
new PropertyValueFactory<>("number"));
TableColumn lastCol = new TableColumn("Email");
lastCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
table.setItems(data);
table.getColumns().addAll(firstCol, secCol,lastCol);
table.setMinWidth(230);
table.setLayoutX(10);
table.setLayoutY(40);
b1.setLayoutX(5);
b1.setLayoutY(5);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st1=cn.createStatement();
database=st1.executeQuery("select * from load");
while(database.next())
{
s = database.getString(1);
s1 = database.getString(2);
s2 = database.getString(3);
data.add(new myData(s,s1,s2));
}  
st.close();
database.close();
cn.close();
}
catch(Exception e)
{
System.out.print(e);

}
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override 
public void handle(ActionEvent e) {
try
{
Connection cn=DriverManager.getConnection("jdbc:odbc:login");
st=cn.createStatement();

database=st1.executeQuery("select * from load");
data.removeAll(data);
while(database.next())
{
s3 = database.getString(1);
s4 = database.getString(2);
s5 = database.getString(3);

data.add(new myData(s3,s4,s5));

}
st.close();
cn.close();
}   catch(Exception ex)
{
System.out.print(ex);
}
}
});
Group root = new Group();
root.getChildren().addAll(table,b1);
stage.setTitle("Table with Database");    
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.show();
}

public static class myData {

private final SimpleStringProperty Name;
private final SimpleStringProperty email;
private final SimpleStringProperty number;
private myData(String fName,String fnum,String email) {
this.Name = new SimpleStringProperty(fName);
this.email = new SimpleStringProperty(email);
this.number = new SimpleStringProperty(fnum);
}


public String getEmail() {
return email.get();
}
public void setEmail(String fName) {
email.set(fName);
}
public String getName() {
return Name.get();
}
public void setName(String fName) {
Name.set(fName);
}
public String getNumber() {
return number.get();
}
public void setNumber(String fName) {
number.set(fName);
}

}
} 

OUTPUT:





Thursday, 15 October 2015

Radio Button In JavaFx

When we want to select one option at a time we can create radio button because it provides series of items and use have choice to select any one when any of the option in radio button is selected actionevent is generated.

toogleGroup:

public final ObjectProperty<ToggleGroup> toggleGroupProperty

By using toogle group property we can detect which option is selected by user.

RADIO BUTTON IN JAVAFX:

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;


public class mainfx extends Application 
{
RadioButton rad = new RadioButton("Small Size");
RadioButton rad1 = new RadioButton("Medium Size");
RadioButton rad2 = new RadioButton("Large Size");
ToggleGroup group = new ToggleGroup();
Label l1 = new Label("SELECT SIZE OF TEXT");
Label l2 = new Label("This is a text");

@Override
public void start(Stage primaryStage)
 {

Group root = new Group();      


primaryStage.setTitle("RADIO BUTTON");
GridPane grid = new GridPane(); 
rad.setToggleGroup(group);
rad1.setToggleGroup(group);
rad2.setToggleGroup(group);
rad.setUserData("First Option");
rad1.setUserData("Second Option");
rad2.setUserData("Third Option");
grid.setHgap(20);
grid.setVgap(20);
grid.add(l1, 1, 0);
grid.add(rad, 0, 1);
grid.add(rad1, 1, 1);
grid.add(rad2, 2, 1);
grid.add(l2, 1, 2);
Scene scene = new Scene(grid, 300, 250);
primaryStage.setScene(scene); 
primaryStage.show();
group.selectedToggleProperty().addListener(new ChangeListener<Toggle>(){
@Override
public void changed(ObservableValue<? extends Toggle> ov,
Toggle old_toggle, Toggle new_toggle) {
if (group.getSelectedToggle() != null) {
}   
String a=(group.getSelectedToggle().getUserData().toString());
if(a.equals("First Option"))
{
l2.setFont(new Font("Arial",15));
}


else if(a.equals("Second Option"))
{
l2.setFont(new Font("Arial",25));
}


else if(a.equals("Third Option"))
{
l2.setFont(new Font("Arial",35)); 
}
}
});

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

}

OUTPUT:



Wednesday, 14 October 2015

Tutorial On Table In JavaFx

In this program i have tried to discuss how to create simple table in java fx with data and without data in our first example we have created table with no data this can be done by creating TableColumn and then adding into a table.

TABLE WITH NO DATA :

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import javafx.stage.Stage;
public class tab extends Application
 {

public static void main(String[] args)
 {
launch(args);
}
@Override
public void start(Stage stage)
 {
Group root = new Group();
stage.setTitle("Simple Table View");
TableView table = new TableView();
table.setEditable(true);
TableColumn firstcol = new TableColumn("Name");
TableColumn secondcol = new TableColumn("Phone Number");
TableColumn thirdcol = new TableColumn("Fax");
table.getColumns().addAll(firstcol,secondcol,thirdcol);
root.getChildren().add(table);
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}


OUTPUT:




TABLE WITH DATA :

In this example we show you how to add data in table with simple method in  we have created class name myData to define a data model for table and an ObservableList array is used to define data that is to be added in table .


import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class tab extends Application {

private final TableView<myData> table = new TableView<>();
private final ObservableList<myData> data =
FXCollections.observableArrayList(new myData("Jones ","8745674","jones@yahoo.com"),
new myData("Adam","9766765","adam@outlook.com"));


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

@Override
public void start(Stage stage) {

TableColumn firstCol = new TableColumn("First Name");
firstCol.setMinWidth(100);
firstCol.setCellValueFactory(
new PropertyValueFactory<>("name"));


TableColumn secCol = new TableColumn("Fax-Number");
secCol.setCellValueFactory(
new PropertyValueFactory<>("number"));


TableColumn lastCol = new TableColumn("Email");
lastCol.setCellValueFactory(
new PropertyValueFactory<>("email"));
table.setItems(data);
table.getColumns().addAll(firstCol, secCol,lastCol);
Group root = new Group();
root.getChildren().add(table);
stage.setTitle("Simple Table With Data");    
Scene scene = new Scene(root, 300, 250);
stage.setScene(scene);
stage.show();
}

public static class myData {
private final SimpleStringProperty Name;
private final SimpleStringProperty email;
private final SimpleStringProperty number;
private myData(String fName,String fnum,String email) {
this.Name = new SimpleStringProperty(fName);
this.email = new SimpleStringProperty(email);
this.number = new SimpleStringProperty(fnum);
}

public String getEmail() {
return email.get();
}

public void setEmail(String fName) {
email.set(fName);
}
public String getName() {
return Name.get();
}
public void setName(String fName) {
Name.set(fName);
}
public String getNumber() {
return number.get();
}
public void setNumber(String fName) {
number.set(fName);
}

}
}

OUTPUT: 






Tuesday, 13 October 2015

Tutorial On Mouse Event In JavaFx

Java fx provides various mouse events,in this tutorials I have used several  mouse events  to explain how to use mouse events in javafx,they are simple and easy to use.Following are the mouse event used in this tutorial.

MOUSE_CLICKED IN JAVAFX:

This event is used to detect the mouse clicked by user.This event provide button like behaviour to a node.

MOUSE_ENTERED_TARGET IN JAVAFX:

This event is occurs when mouse enter in the region of node.

MOUSE_DRAGED IN JAVAFX:

In this example we have used this event for dragging a button this event is used to detects the drag by a user on  a node.

CODING:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class mainfx extends Application
{


@Override
public void start(Stage primaryStage)
{

StackPane root = new StackPane();

Button btn = new Button();

btn.setText("BUTTON");


btn.setOnMouseClicked((MouseEvent)->
{
System.out.println("Mouse clicked");
}
);
btn.setOnMouseEntered((MouseEvent)->
{
System.out.println("Mouse Entered");
}
);
btn.setOnMouseExited((MouseEvent)->
{
System.out.println("Mouse Exit");
}
);
btn.setOnMouseReleased((MouseEvent)->
{
System.out.println("Mouse Release");
}
);
btn.setOnMouseDragged((MouseEvent me)->
{
double x = me.getX();
double y = me.getY();
btn.setTranslateX(x);
btn.setTranslateY(y);
}
);

root.getChildren().add(btn);


Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Mouse Event");
primaryStage.setScene(scene);
primaryStage.show();
}


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

}

Saturday, 3 October 2015

Tutorial On Slider In Java Fx

The Slider Control is used to display a continuous  range of  numeric values  and allows the user to interact with the control graphically.In this tutorial  I have used slider to change the width and height of image.


ValueProperty method:

public final DoubleProperty valueProperty()

This method is used to detect the  current value represented by this Slider. This value must always be between minimum  and maximum value.


CODING:

import javafx.application.Application;import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;


public class mainfx extends Application
 {

Slider slider = new Slider(175,500,250);
Slider slider1 = new Slider(150,300,150);
Label l1 = new Label("Width Of Image");
Label l2 = new Label("Height Of Image");
Image image = new Image ("face.jpg");
ImageView iv = new ImageView();
@Override
public void start(Stage primaryStage)
{

slider.setShowTickLabels(true);
slider1.setShowTickLabels(true);
iv.setFitHeight(150);
iv.setImage(image);
StackPane grid = new StackPane();
FlowPane flow = new FlowPane();
FlowPane flow1 = new FlowPane();
flow.getChildren().add(iv);
flow1.getChildren().addAll(l1,slider,l2,slider1);
flow1.setPrefWrapLength(110);
flow1.setVgap(20);
BorderPane border = new BorderPane();
border.setTop(flow);
border.setLeft(flow1);
Scene scene = new Scene(border, 600, 350,Color.WHITE);
primaryStage.setTitle("Slider In Javafx");
primaryStage.setScene(scene);
slider.valueProperty().addListener(new ChangeListener<Number>()
 {
@Override
public void changed(ObservableValue<? extends Number> observable,
Number oldvalue, Number newvalue)
{
int ne = newvalue.intValue();
iv.setFitWidth(ne*4);
}
});
slider1.valueProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable,
Number oldvalue, Number newvalue)
 {
int ne1 = newvalue.intValue();
iv.setFitHeight(ne1);
}
});


primaryStage.show();
}

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

}

OUTPUT:


Friday, 25 September 2015

Tutorial On Menubar With Custom MenuItem In JavaFx

This tutorial will explain you how to create menu bar,how to add menu item in menu bar,how to  and how to add custom menu item in menu bar in java fx.Following are the steps for creating menu bar with menu items.

  1. Create menu bar and then add it on node.
  2. Create menu and add it into menu bar.
  3. Create menu item and add it in menu.
  4. If you want to create sub-menu item then first create it and then add into a menu item.



CODING:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CustomMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Slider;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class fxmain extends Application 
{
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("First menu");
MenuItem firstmenuitem = new MenuItem("FirstMenu Item");
Menu secondmenuitem = new Menu("SecondMenu Item");
MenuItem thirdmenuitem = new MenuItem("ThirdMenu Item");
MenuItem fourthmenuitem = new MenuItem("ThirdMenu Item");
MenuItem firstsubmenu = new MenuItem("FirstSubMenu");
MenuItem secondsubmenu = new MenuItem("SeconSubMenu");
CustomMenuItem slide = new CustomMenuItem(new Slider());

@Override
public void start(Stage primaryStage)
 {

menuBar.getMenus().add(menu);
menu.getItems().addAll(firstmenuitem,secondmenuitem,thirdmenuitem,new  SeparatorMenuItem(),slide);
secondmenuitem.getItems().addAll(firstsubmenu,secondsubmenu);

Group root = new Group();
root.getChildren().add(menuBar);
menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
Scene scene = new Scene(root, 300, 250,Color.OLDLACE);

primaryStage.setTitle("Menu In Fx");
primaryStage.setScene(scene);
primaryStage.show();
}


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

}

OUTPUT: