Wednesday, 14 September 2016

JavaFX: Check Box

JavaFx:How to use checkbox in Java Fx?


For creating a file chooser you should have a structure as provided below in JavaFx project.

We have created this checkbox Java Fx programme to select multiple values from given options.You can modify code according to your requirement.

Main.java

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;



public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,300,300);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

MainController.java

package application;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;

public class MainController {
@FXML
private CheckBox cb1;
@FXML
private CheckBox cb2;
@FXML
private CheckBox cb3;
@FXML
private CheckBox cb4;
@FXML
private Label lbcount;

@FXML
private Label lbdisp;
public void checkevent(ActionEvent event){
int count=0;
String message="";
if(cb1.isSelected()){
count++;
message=message+cb1.getText()+"\n";
}
if(cb2.isSelected()){
count++;
message=message+cb2.getText()+"\n";
}
if(cb3.isSelected()){
count++;
message=message+cb3.getText()+"\n";
}
if(cb4.isSelected()){
count++;
message=message+cb4.getText()+"\n";
}
lbcount.setText("Items selected : " +count);
lbdisp.setText(message);
}
}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <CheckBox fx:id="cb1" layoutX="56.0" layoutY="48.0" mnemonicParsing="false" onAction="#checkevent" text="Select A" />
      <CheckBox fx:id="cb2" layoutX="56.0" layoutY="85.0" mnemonicParsing="false" onAction="#checkevent" text="Select B" />
      <CheckBox fx:id="cb3" layoutX="56.0" layoutY="123.0" mnemonicParsing="false" onAction="#checkevent" text="Select C" />
      <CheckBox fx:id="cb4" layoutX="56.0" layoutY="168.0" mnemonicParsing="false" onAction="#checkevent" text="Select D" />
      <Label fx:id="lbcount" layoutX="56.0" layoutY="223.0" prefHeight="43.0" prefWidth="152.0" />
      <Label fx:id="lbdisp" layoutX="148.0" layoutY="49.0" prefHeight="141.0" prefWidth="138.0" />
   </children>
</AnchorPane>

Output


Reference & More


Monday, 12 September 2016

JavaFX: File Chooser

JavaFx:How to choose single file and multiple files in Java Fx and display in a list view?


For creating a file chooser you should have a structure as provided below in JavaFx project.
We have created this file chooser to select single or multiple files from a specific directory and display the file names in  a list view. You may pass the directory of the file or name of the file to a Java class.

Main.java

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;



public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

MainController.java

package application;

import java.io.File;
import java.util.List;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;


public class MainController  {

@FXML
private Button btn1;
@FXML
private Button btn2;
@FXML
private ListView listview;
public void buttonaction1(ActionEvent event){
FileChooser fc =new FileChooser();
//Open files of a default directory
fc.setInitialDirectory(new File("C:\\Users\\sridhar mohanty\\Desktop"));
//Filter out a specific format
fc.getExtensionFilters().addAll(new ExtensionFilter("Microsoft Word Files","*.doc"));
File SelectedFile=fc.showOpenDialog(null);
if(SelectedFile!=null){
listview.getItems().add(SelectedFile.getName());
//Display absolute path
//listview.getItems().add(SelectedFile.getAbsolutePath());
}else{
System.out.println("File is not valid");
}
}

public void buttonaction2(ActionEvent event){
FileChooser fc =new FileChooser();
//Open files of a default directory
fc.setInitialDirectory(new File("C:\\Users\\sridhar mohanty\\Desktop"));
//Filter out a specific format
fc.getExtensionFilters().addAll(new ExtensionFilter("Microsoft Word Files","*.doc"));
List<File> SelectedFiles=fc.showOpenMultipleDialog(null);
if(SelectedFiles!=null){
for(int i=0;i<SelectedFiles.size();i++)
listview.getItems().add(SelectedFiles.get(i).getName());
}else{
System.out.println("File is not valid");
}
}
}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <Button fx:id="btn1" layoutX="41.0" layoutY="138.0" mnemonicParsing="false" onAction="#buttonaction1" text="Select File" />
      <Button fx:id="btn2" layoutX="18.0" layoutY="259.0" mnemonicParsing="false" onAction="#buttonaction2" text="Select Multiple File" />
      <ListView fx:id="listview" layoutX="168.0" layoutY="66.0" prefHeight="282.0" prefWidth="200.0" />
   </children>
</AnchorPane>

Output


Reference & More

JavaFX: Tree View

JavaFx:How to create a Tree View in Java Fx?

For creating Tree View you should have a structure as provided below in JavaFx project.
We have created this Tree View to display the selected folder in console as well as also added folder image to tree. You can modify code according to your requirement.

Main.java

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

MainController.java


package application;

import java.net.URL;
import java.util.ResourceBundle;


import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;

public class MainController implements Initializable {
@FXML
TreeView <String> treeview;
Image img=new Image(getClass().getResourceAsStream("/img/folder.png"));
Image img1=new Image(getClass().getResourceAsStream("/img/folder1.jpg"));
@SuppressWarnings("unchecked")
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
TreeItem<String> root= new TreeItem<>("Root",new ImageView(img));
//Root node will be displayed as expanded format.
root.setExpanded(true);
TreeItem<String> nodea= new TreeItem<>("Node A",new ImageView(img));
TreeItem<String> nodeb= new TreeItem<>("Node B",new ImageView(img));
TreeItem<String> nodec= new TreeItem<>("Node C",new ImageView(img));
root.getChildren().addAll(nodea,nodeb,nodec);
//sub nodes of node A.
TreeItem<String> nodea1= new TreeItem<>("Node A1",new ImageView(img1));
TreeItem<String> nodea2= new TreeItem<>("Node A2",new ImageView(img1));
TreeItem<String> nodea3= new TreeItem<>("Node A3",new ImageView(img1));
nodea.getChildren().addAll(nodea1,nodea2,nodea3);
treeview.setRoot(root);

}
//Action event to show the value of node when clicked.
public void mouseclick(MouseEvent mouseclickevent){
TreeItem<String> item = treeview.getSelectionModel().getSelectedItem();
System.out.println(item.getValue());
}

}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TreeView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <TreeView fx:id="treeview" layoutX="50.0" layoutY="50.0" onContextMenuRequested="#mouseclick" onMouseClicked="#mouseclick" prefHeight="200.0" prefWidth="200.0" />
   </children>
</AnchorPane>

Output



Reference & More


Tuesday, 30 August 2016

JavaFX: List View

JavaFx:How to create a List View in Java Fx?

For generating List View you should have a structure as provided below in JavaFx project.
We have created this List View to display the selected value in console. You can modify code according to your requirement.

Main.java

package application_listview;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application_listview/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

MainController.java

package application_listview;


import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;

public class MainController implements Initializable  {
@FXML
public ListView<String> listview;
ObservableList<String> list=FXCollections.observableArrayList("one","two","three","four");
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
listview.setItems(list);
listview.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
public void buttonAction(ActionEvent event){
ObservableList<String> names;
names=listview.getSelectionModel().getSelectedItems();
for(String name :names){
System.out.println(name);
}
}

}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application_listview.MainController">
   <children>
      <ListView fx:id="listview" layoutX="50.0" layoutY="72.0" prefHeight="200.0" prefWidth="200.0" />
      <Button layoutX="124.0" layoutY="25.0" mnemonicParsing="false" onAction="#buttonAction" text="Button" />
   </children>
</AnchorPane>

Output


Reference & More

Monday, 29 August 2016

JavaFX: Combo box

JavaFx:How to create a combo box in Java Fx?


For generating combo box you should have a structure as provided below in JavaFx project.



We have created this combo box to display the selected value in a label. You can modify code according to your requirement.

Main.java

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;


public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root=FXMLLoader.load(getClass().getResource("/application/Main.fxml")); 
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

MainController.java

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;

public class MainController implements Initializable{
@FXML
public Label mylabel;
@FXML
public ComboBox<String> combobox;
ObservableList<String> list=FXCollections.observableArrayList("first","second","third");
@Override
public void initialize(URL location, ResourceBundle resources) {
combobox.setItems(list);
}
public void combochanged(){
mylabel.setText(combobox.getValue());
}
}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <children>
      <ComboBox fx:id="combobox" layoutX="50.0" layoutY="101.0" onAction="#combochanged" prefHeight="49.0" prefWidth="194.0" promptText="select Name" />
      <Label fx:id="mylabel" layoutX="53.0" layoutY="178.0" prefHeight="34.0" prefWidth="187.0" text="Label" />
   </children>
</AnchorPane>

Output

You will get an output display as provided below.

Reference & More



Tuesday, 23 August 2016

Java:-Find particular substring from a .txt file.

Java:-The below mentioned code enables user to find a particular length of a string whenever required from a file(.txt file or something like that).

Example:-I have a requirement to extract 10 digit card number which was written like CardNumber:="'1234567890".

Note:-Put your sample txt file in workspace or modify file reader argument accordingly.

Code:-

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class PatternMatch {

 public static void main(String[] args) {
  try {
   BufferedReader br= new BufferedReader(new FileReader("C:\\workspace\\newfile.txt"));
   String line;
   int lastindex=0;
   try {
    while((line=br.readLine())!=null){
    while(lastindex!=-1){
   lastindex = line.indexOf("CardNumber",lastindex);
    if(lastindex!=-1){
    String substr=line.substring(lastindex+14,lastindex+14+10);
     System.out.println(substr);
    lastindex=lastindex+14+10;
     }
     }
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }

}

Monday, 22 August 2016

Java:-Read from text file using buffered reader.

Java:-Read from a text file in Java

This code enables you to read from a .txt text file format."C:\\workspace\\newfile.txt" is a sample workspace . Replace path with required path.


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

public static void main(String[] args) {
try {
BufferedReader br= new BufferedReader(new
FileReader("C:\\workspace\\newfile.txt"));
String line;
try {
while((line=br.readLine())!=null){
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}